home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / cpp / cccp.c < prev    next >
C/C++ Source or Header  |  1997-12-07  |  286KB  |  10,488 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2.    Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3.    Written by Paul Rubin, June 1986
  4.    Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6.    Modified by Andrew M. Bishop to provide better input to
  7.    C documentation program `cxref' 1995,1996.
  8.    All AMB hacks are indicated as such in the code (grep AMB).
  9.  
  10. This program is free software; you can redistribute it and/or modify it
  11. under the terms of the GNU General Public License as published by the
  12. Free Software Foundation; either version 2, or (at your option) any
  13. later version.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, 59 Temple Place - Suite 330,
  23. Boston, MA 02111-1307, USA.
  24.  
  25.  In other words, you are welcome to use, share and improve this program.
  26.  You are forbidden to forbid anyone else to use, share and improve
  27.  what you give them.   Help stamp out software-hoarding!  */
  28.  
  29. typedef unsigned char U_CHAR;
  30.  
  31. #ifdef EMACS
  32. #define NO_SHORTNAMES
  33. #include "../src/config.h"
  34. #ifdef open
  35. #undef open
  36. #undef read
  37. #undef write
  38. #endif /* open */
  39. #endif /* EMACS */
  40.  
  41. /* The macro EMACS is defined when cpp is distributed as part of Emacs,
  42.    for the sake of machines with limited C compilers.  */
  43. #ifndef EMACS
  44. #include "config.h"
  45. #endif /* not EMACS */
  46.  
  47. #ifndef STANDARD_INCLUDE_DIR
  48. #define STANDARD_INCLUDE_DIR "/usr/include"
  49. #endif
  50.  
  51. #ifndef LOCAL_INCLUDE_DIR
  52. #define LOCAL_INCLUDE_DIR "/usr/local/include"
  53. #endif
  54.  
  55. #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE.  */
  56. #ifdef __STDC__
  57. #define PTR_INT_TYPE ptrdiff_t
  58. #else
  59. #define PTR_INT_TYPE long
  60. #endif
  61. #endif /* 0 */
  62.  
  63. #include "pcp.h"
  64.  
  65. /* By default, colon separates directories in a path.  */
  66. #ifndef PATH_SEPARATOR
  67. #define PATH_SEPARATOR ':'
  68. #endif
  69.  
  70. #include <sys/types.h>
  71. #include <sys/stat.h>
  72. #include <ctype.h>
  73. #include <stdio.h>
  74. #include <signal.h>
  75.  
  76. /* The following symbols should be autoconfigured:
  77.     HAVE_FCNTL_H
  78.     HAVE_STDLIB_H
  79.     HAVE_SYS_TIME_H
  80.     HAVE_UNISTD_H
  81.     STDC_HEADERS
  82.     TIME_WITH_SYS_TIME
  83.    In the mean time, we'll get by with approximations based
  84.    on existing GCC configuration symbols.  */
  85.  
  86. #ifdef POSIX
  87. # ifndef HAVE_STDLIB_H
  88. # define HAVE_STDLIB_H 1
  89. # endif
  90. # ifndef HAVE_UNISTD_H
  91. # define HAVE_UNISTD_H 1
  92. # endif
  93. # ifndef STDC_HEADERS
  94. # define STDC_HEADERS 1
  95. # endif
  96. #endif /* defined (POSIX) */
  97.  
  98. #if defined (POSIX) || (defined (USG) && !defined (VMS))
  99. # ifndef HAVE_FCNTL_H
  100. # define HAVE_FCNTL_H 1
  101. # endif
  102. #endif
  103.  
  104. #ifndef RLIMIT_STACK
  105. # include <time.h>
  106. #else
  107. # if TIME_WITH_SYS_TIME
  108. #  include <sys/time.h>
  109. #  include <time.h>
  110. # else
  111. #  if HAVE_SYS_TIME_H
  112. #   include <sys/time.h>
  113. #  else
  114. #   include <time.h>
  115. #  endif
  116. # endif
  117. # include <sys/resource.h>
  118. #endif
  119.  
  120. #if HAVE_FCNTL_H
  121. # include <fcntl.h>
  122. #endif
  123.  
  124. /* This defines "errno" properly for VMS, and gives us EACCES. */
  125. #include <errno.h>
  126.  
  127. #if HAVE_STDLIB_H
  128. # include <stdlib.h>
  129. #else
  130. char *getenv ();
  131. #endif
  132.  
  133. #if STDC_HEADERS
  134. # include <string.h>
  135. # ifndef bcmp
  136. # define bcmp(a, b, n) memcmp (a, b, n)
  137. # endif
  138. # ifndef bcopy
  139. # define bcopy(s, d, n) memcpy (d, s, n)
  140. # endif
  141. # ifndef bzero
  142. # define bzero(d, n) memset (d, 0, n)
  143. # endif
  144. #else /* !STDC_HEADERS */
  145. char *index ();
  146. char *rindex ();
  147.  
  148. # if !defined (BSTRING) && (defined (USG) || defined (VMS))
  149.  
  150. #  ifndef bcmp
  151. #  define bcmp my_bcmp
  152. static int
  153. my_bcmp (a, b, n)
  154.      register char *a;
  155.      register char *b;
  156.      register unsigned n;
  157. {
  158.    while (n-- > 0)
  159.      if (*a++ != *b++)
  160.        return 1;
  161.  
  162.    return 0;
  163. }
  164. #  endif /* !defined (bcmp) */
  165.  
  166. #  ifndef bcopy
  167. #  define bcopy my_bcopy
  168. static void
  169. my_bcopy (s, d, n)
  170.      register char *s;
  171.      register char *d;
  172.      register unsigned n;
  173. {
  174.   while (n-- > 0)
  175.     *d++ = *s++;
  176. }
  177. #  endif /* !defined (bcopy) */
  178.  
  179. #  ifndef bzero
  180. #  define bzero my_bzero
  181. static void
  182. my_bzero (b, length)
  183.      register char *b;
  184.      register unsigned length;
  185. {
  186.   while (length-- > 0)
  187.     *b++ = 0;
  188. }
  189. #  endif /* !defined (bzero) */
  190.  
  191. # endif /* !defined (BSTRING) && (defined (USG) || defined (VMS)) */
  192. #endif /* ! STDC_HEADERS */
  193.  
  194. #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6)
  195. # define __attribute__(x)
  196. #endif
  197.  
  198. #ifndef PROTO
  199. # if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  200. #  define PROTO(ARGS) ARGS
  201. # else
  202. #  define PROTO(ARGS) ()
  203. # endif
  204. #endif
  205.  
  206. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  207. # include <stdarg.h>
  208. # define VA_START(va_list, var) va_start (va_list, var)
  209. # define PRINTF_ALIST(msg) char *msg, ...
  210. # define PRINTF_DCL(msg)
  211. # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (printf, m, n)))
  212. #else
  213. # include <varargs.h>
  214. # define VA_START(va_list, var) va_start (va_list)
  215. # define PRINTF_ALIST(msg) msg, va_alist
  216. # define PRINTF_DCL(msg) char *msg; va_dcl
  217. # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (printf, m, n)))
  218. # define vfprintf(file, msg, args) \
  219.     { \
  220.       char *a0 = va_arg(args, char *); \
  221.       char *a1 = va_arg(args, char *); \
  222.       char *a2 = va_arg(args, char *); \
  223.       char *a3 = va_arg(args, char *); \
  224.       fprintf (file, msg, a0, a1, a2, a3); \
  225.     }
  226. #endif
  227.  
  228. #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
  229. #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
  230. #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
  231.  
  232. #if HAVE_UNISTD_H
  233. # include <unistd.h>
  234. #endif
  235.  
  236. /* VMS-specific definitions */
  237. #ifdef VMS
  238. #include <descrip.h>
  239. #define O_RDONLY    0    /* Open arg for Read/Only  */
  240. #define O_WRONLY    1    /* Open arg for Write/Only */
  241. #define read(fd,buf,size)    VMS_read (fd,buf,size)
  242. #define write(fd,buf,size)    VMS_write (fd,buf,size)
  243. #define open(fname,mode,prot)    VMS_open (fname,mode,prot)
  244. #define fopen(fname,mode)    VMS_fopen (fname,mode)
  245. #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
  246. #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt)
  247. #define fstat(fd,stbuf)        VMS_fstat (fd,stbuf)
  248. static int VMS_fstat (), VMS_stat ();
  249. static char * VMS_strncat ();
  250. static int VMS_read ();
  251. static int VMS_write ();
  252. static int VMS_open ();
  253. static FILE * VMS_fopen ();
  254. static FILE * VMS_freopen ();
  255. static void hack_vms_include_specification ();
  256. typedef struct { unsigned :16, :16, :16; } vms_ino_t;
  257. #define ino_t vms_ino_t
  258. #define INCLUDE_LEN_FUDGE 10    /* leave room for VMS syntax conversion */
  259. #ifdef __GNUC__
  260. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  261. #endif /* __GNUC__ */
  262. #endif /* VMS */
  263.  
  264. #ifndef O_RDONLY
  265. #define O_RDONLY 0
  266. #endif
  267.  
  268. #undef MIN
  269. #undef MAX
  270. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  271. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  272.  
  273. /* Find the largest host integer type and set its size and type.  */
  274.  
  275. #ifndef HOST_BITS_PER_WIDE_INT
  276.  
  277. #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
  278. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
  279. #define HOST_WIDE_INT long
  280. #else
  281. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
  282. #define HOST_WIDE_INT int
  283. #endif
  284.  
  285. #endif
  286.  
  287. #ifndef S_ISREG
  288. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  289. #endif
  290.  
  291. #ifndef S_ISDIR
  292. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  293. #endif
  294.  
  295. /* Define a generic NULL if one hasn't already been defined.  */
  296.  
  297. #ifndef NULL
  298. #define NULL 0
  299. #endif
  300.  
  301. #ifndef GENERIC_PTR
  302. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  303. #define GENERIC_PTR void *
  304. #else
  305. #define GENERIC_PTR char *
  306. #endif
  307. #endif
  308.  
  309. #ifndef NULL_PTR
  310. #define NULL_PTR ((GENERIC_PTR)0)
  311. #endif
  312.  
  313. #ifndef INCLUDE_LEN_FUDGE
  314. #define INCLUDE_LEN_FUDGE 0
  315. #endif
  316.  
  317. /* External declarations.  */
  318.  
  319. extern char *version_string;
  320. #ifndef VMS
  321. #ifndef HAVE_STRERROR
  322. extern int sys_nerr;
  323. #if defined(bsd4_4)
  324. extern const char *const sys_errlist[];
  325. #else
  326. extern char *sys_errlist[];
  327. #endif
  328. #else    /* HAVE_STRERROR */
  329. char *strerror ();
  330. #endif
  331. #else    /* VMS */
  332. char *strerror (int,...);
  333. #endif
  334. int parse_escape PROTO((char **));
  335. HOST_WIDE_INT parse_c_expression PROTO((char *));
  336.  
  337. #ifndef errno
  338. extern int errno;
  339. #endif
  340.  
  341. /* Name under which this program was invoked.  */
  342.  
  343. static char *progname;
  344.  
  345. /* Nonzero means use extra default include directories for C++.  */
  346.  
  347. static int cplusplus;
  348.  
  349. /* Nonzero means handle cplusplus style comments */
  350.  
  351. static int cplusplus_comments;
  352.  
  353. /* Nonzero means handle #import, for objective C.  */
  354.  
  355. static int objc;
  356.  
  357. /* Nonzero means this is an assembly file, and allow
  358.    unknown directives, which could be comments.  */
  359.  
  360. static int lang_asm;
  361.  
  362. /* Current maximum length of directory names in the search path
  363.    for include files.  (Altered as we get more of them.)  */
  364.  
  365. static int max_include_len;
  366.  
  367. /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
  368.  
  369. static int for_lint = 0;
  370.  
  371. /* Nonzero means copy comments into the output file.  */
  372.  
  373. static int put_out_comments = 0;
  374.  
  375. /* Nonzero means don't process the ANSI trigraph sequences.  */
  376.  
  377. static int no_trigraphs = 0;
  378.  
  379. /* Nonzero means print the names of included files rather than
  380.    the preprocessed output.  1 means just the #include "...",
  381.    2 means #include <...> as well.  */
  382.  
  383. static int print_deps = 0;
  384.  
  385. /* Nonzero if missing .h files in -M output are assumed to be generated
  386.    files and not errors.  */
  387.  
  388. static int print_deps_missing_files = 0;
  389.  
  390. /* Nonzero means print names of header files (-H).  */
  391.  
  392. static int print_include_names = 0;
  393.  
  394. /* Nonzero means don't output line number information.  */
  395.  
  396. static int no_line_directives;
  397.  
  398. /* Nonzero means output the text in failing conditionals,
  399.    inside #failed ... #endfailed.  */
  400.  
  401. static int output_conditionals;
  402.  
  403. /* dump_only means inhibit output of the preprocessed text
  404.              and instead output the definitions of all user-defined
  405.              macros in a form suitable for use as input to cccp.
  406.    dump_names means pass #define and the macro name through to output.
  407.    dump_definitions means pass the whole definition (plus #define) through
  408. */
  409.  
  410. static enum {dump_none, dump_only, dump_names, dump_definitions}
  411.      dump_macros = dump_none;
  412.  
  413. /* Start new option added by AMB */
  414.  
  415. /* Nonzero means that the #include lines are to be passed through to
  416.    output.
  417. */
  418.  
  419. static int dump_includes = 0;
  420.  
  421. /* End new option added by AMB */
  422.  
  423. /* Nonzero means pass all #define and #undef directives which we actually
  424.    process through to the output stream.  This feature is used primarily
  425.    to allow cc1 to record the #defines and #undefs for the sake of
  426.    debuggers which understand about preprocessor macros, but it may
  427.    also be useful with -E to figure out how symbols are defined, and
  428.    where they are defined.  */
  429. static int debug_output = 0;
  430.  
  431. /* Nonzero indicates special processing used by the pcp program.  The
  432.    special effects of this mode are: 
  433.      
  434.      Inhibit all macro expansion, except those inside #if directives.
  435.  
  436.      Process #define directives normally, and output their contents 
  437.      to the output file.
  438.  
  439.      Output preconditions to pcp_outfile indicating all the relevant
  440.      preconditions for use of this file in a later cpp run.
  441. */
  442. static FILE *pcp_outfile;
  443.  
  444. /* Nonzero means we are inside an IF during a -pcp run.  In this mode
  445.    macro expansion is done, and preconditions are output for all macro
  446.    uses requiring them. */
  447. static int pcp_inside_if;
  448.  
  449. /* Nonzero means never to include precompiled files.
  450.    This is 1 since there's no way now to make precompiled files,
  451.    so it's not worth testing for them.  */
  452. static int no_precomp = 1;
  453.  
  454. /* Nonzero means give all the error messages the ANSI standard requires.  */
  455.  
  456. int pedantic;
  457.  
  458. /* Nonzero means try to make failure to fit ANSI C an error.  */
  459.  
  460. static int pedantic_errors;
  461.  
  462. /* Nonzero means don't print warning messages.  -w.  */
  463.  
  464. static int inhibit_warnings = 0;
  465.  
  466. /* Nonzero means warn if slash-star appears in a comment.  */
  467.  
  468. static int warn_comments;
  469.  
  470. /* Nonzero means warn if a macro argument is (or would be)
  471.    stringified with -traditional.  */
  472.  
  473. static int warn_stringify;
  474.  
  475. /* Nonzero means warn if there are any trigraphs.  */
  476.  
  477. static int warn_trigraphs;
  478.  
  479. /* Nonzero means warn if #import is used.  */
  480.  
  481. static int warn_import = 1;
  482.  
  483. /* Nonzero means turn warnings into errors.  */
  484.  
  485. static int warnings_are_errors;
  486.  
  487. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  488.  
  489. int traditional;
  490.  
  491. /* Nonzero causes output not to be done,
  492.    but directives such as #define that have side effects
  493.    are still obeyed.  */
  494.  
  495. static int no_output;
  496.  
  497. /* Nonzero means this file was included with a -imacros or -include
  498.    command line and should not be recorded as an include file.  */
  499.  
  500. static int no_record_file;
  501.  
  502. /* Nonzero means that we have finished processing the command line options.
  503.    This flag is used to decide whether or not to issue certain errors
  504.    and/or warnings.  */
  505.  
  506. static int done_initializing = 0;
  507.  
  508. /* Line where a newline was first seen in a string constant.  */
  509.  
  510. static int multiline_string_line = 0;
  511.  
  512. /* I/O buffer structure.
  513.    The `fname' field is nonzero for source files and #include files
  514.    and for the dummy text used for -D and -U.
  515.    It is zero for rescanning results of macro expansion
  516.    and for expanding macro arguments.  */
  517. #define INPUT_STACK_MAX 400
  518. static struct file_buf {
  519.   char *fname;
  520.   /* Filename specified with #line directive.  */
  521.   char *nominal_fname;
  522.   /* Record where in the search path this file was found.
  523.      For #include_next.  */
  524.   struct file_name_list *dir;
  525.   int lineno;
  526.   int length;
  527.   U_CHAR *buf;
  528.   U_CHAR *bufp;
  529.   /* Macro that this level is the expansion of.
  530.      Included so that we can reenable the macro
  531.      at the end of this level.  */
  532.   struct hashnode *macro;
  533.   /* Value of if_stack at start of this file.
  534.      Used to prohibit unmatched #endif (etc) in an include file.  */
  535.   struct if_stack *if_stack;
  536.   /* Object to be freed at end of input at this level.  */
  537.   U_CHAR *free_ptr;
  538.   /* True if this is a header file included using <FILENAME>.  */
  539.   char system_header_p;
  540. } instack[INPUT_STACK_MAX];
  541.  
  542. static int last_error_tick;       /* Incremented each time we print it.  */
  543. static int input_file_stack_tick;  /* Incremented when the status changes.  */
  544.  
  545. /* Current nesting level of input sources.
  546.    `instack[indepth]' is the level currently being read.  */
  547. static int indepth = -1;
  548. #define CHECK_DEPTH(code) \
  549.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  550.     {                                    \
  551.       error_with_line (line_for_error (instack[indepth].lineno),    \
  552.                "macro or `#include' recursion too deep");    \
  553.       code;                                \
  554.     }
  555.  
  556. /* Current depth in #include directives that use <...>.  */
  557. static int system_include_depth = 0;
  558.  
  559. typedef struct file_buf FILE_BUF;
  560.  
  561. /* The output buffer.  Its LENGTH field is the amount of room allocated
  562.    for the buffer, not the number of chars actually present.  To get
  563.    that, subtract outbuf.buf from outbuf.bufp. */
  564.  
  565. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  566. static FILE_BUF outbuf;
  567.  
  568. /* Grow output buffer OBUF points at
  569.    so it can hold at least NEEDED more chars.  */
  570.  
  571. #define check_expand(OBUF, NEEDED)  \
  572.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  573.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  574.  
  575. struct file_name_list
  576.   {
  577.     struct file_name_list *next;
  578.     char *fname;
  579.     /* If the following is nonzero, it is a macro name.
  580.        Don't include the file again if that macro is defined.  */
  581.     U_CHAR *control_macro;
  582.     /* If the following is nonzero, it is a C-language system include
  583.        directory.  */
  584.     int c_system_include_path;
  585.     /* Mapping of file names for this directory.  */
  586.     struct file_name_map *name_map;
  587.     /* Non-zero if name_map is valid.  */
  588.     int got_name_map;
  589.   };
  590.  
  591. /* #include "file" looks in source file dir, then stack. */
  592. /* #include <file> just looks in the stack. */
  593. /* -I directories are added to the end, then the defaults are added. */
  594. /* The */
  595. static struct default_include {
  596.   char *fname;            /* The name of the directory.  */
  597.   int cplusplus;        /* Only look here if we're compiling C++.  */
  598.   int cxx_aware;        /* Includes in this directory don't need to
  599.                    be wrapped in extern "C" when compiling
  600.                    C++.  */
  601. } include_defaults_array[]
  602. #ifdef INCLUDE_DEFAULTS
  603.   = INCLUDE_DEFAULTS;
  604. #else
  605.   = {
  606.     /* Pick up GNU C++ specific include files.  */
  607.     { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
  608. #ifdef CROSS_COMPILE
  609.     /* This is the dir for fixincludes.  Put it just before
  610.        the files that we fix.  */
  611.     { GCC_INCLUDE_DIR, 0, 0 },
  612.     /* For cross-compilation, this dir name is generated
  613.        automatically in Makefile.in.  */
  614.     { CROSS_INCLUDE_DIR, 0, 0 },
  615.     /* This is another place that the target system's headers might be.  */
  616.     { TOOL_INCLUDE_DIR, 0, 0 },
  617. #else /* not CROSS_COMPILE */
  618.     /* This should be /usr/local/include and should come before
  619.        the fixincludes-fixed header files.  */
  620.     { LOCAL_INCLUDE_DIR, 0, 1 },
  621.     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
  622.        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
  623.     { TOOL_INCLUDE_DIR, 0, 0 },
  624.     /* This is the dir for fixincludes.  Put it just before
  625.        the files that we fix.  */
  626.     { GCC_INCLUDE_DIR, 0, 0 },
  627.     /* Some systems have an extra dir of include files.  */
  628. #ifdef SYSTEM_INCLUDE_DIR
  629.     { SYSTEM_INCLUDE_DIR, 0, 0 },
  630. #endif
  631.     { STANDARD_INCLUDE_DIR, 0, 0 },
  632. #endif /* not CROSS_COMPILE */
  633.     { 0, 0, 0 }
  634.     };
  635. #endif /* no INCLUDE_DEFAULTS */
  636.  
  637. /* The code looks at the defaults through this pointer, rather than through
  638.    the constant structure above.  This pointer gets changed if an environment
  639.    variable specifies other defaults.  */
  640. static struct default_include *include_defaults = include_defaults_array;
  641.  
  642. static struct file_name_list *include = 0;    /* First dir to search */
  643.     /* First dir to search for <file> */
  644. /* This is the first element to use for #include <...>.
  645.    If it is 0, use the entire chain for such includes.  */
  646. static struct file_name_list *first_bracket_include = 0;
  647. /* This is the first element in the chain that corresponds to
  648.    a directory of system header files.  */
  649. static struct file_name_list *first_system_include = 0;
  650. static struct file_name_list *last_include = 0;    /* Last in chain */
  651.  
  652. /* Chain of include directories to put at the end of the other chain.  */
  653. static struct file_name_list *after_include = 0;
  654. static struct file_name_list *last_after_include = 0;    /* Last in chain */
  655.  
  656. /* Chain to put at the start of the system include files.  */
  657. static struct file_name_list *before_system = 0;
  658. static struct file_name_list *last_before_system = 0;    /* Last in chain */
  659.  
  660. /* List of included files that contained #pragma once.  */
  661. static struct file_name_list *dont_repeat_files = 0;
  662.  
  663. /* List of other included files.
  664.    If ->control_macro if nonzero, the file had a #ifndef
  665.    around the entire contents, and ->control_macro gives the macro name.  */
  666. static struct file_name_list *all_include_files = 0;
  667.  
  668. /* Directory prefix that should replace `/usr' in the standard
  669.    include file directories.  */
  670. static char *include_prefix;
  671.  
  672. /* Global list of strings read in from precompiled files.  This list
  673.    is kept in the order the strings are read in, with new strings being
  674.    added at the end through stringlist_tailp.  We use this list to output
  675.    the strings at the end of the run. 
  676. */
  677. static STRINGDEF *stringlist;
  678. static STRINGDEF **stringlist_tailp = &stringlist;
  679.  
  680.  
  681. /* Structure returned by create_definition */
  682. typedef struct macrodef MACRODEF;
  683. struct macrodef
  684. {
  685.   struct definition *defn;
  686.   U_CHAR *symnam;
  687.   int symlen;
  688. };
  689.  
  690. enum sharp_token_type {
  691.   NO_SHARP_TOKEN = 0,        /* token not present */
  692.  
  693.   SHARP_TOKEN = '#',        /* token spelled with # only */
  694.   WHITE_SHARP_TOKEN,        /* token spelled with # and white space */
  695.  
  696.   PERCENT_COLON_TOKEN = '%',    /* token spelled with %: only */
  697.   WHITE_PERCENT_COLON_TOKEN    /* token spelled with %: and white space */
  698. };
  699.  
  700. /* Structure allocated for every #define.  For a simple replacement
  701.    such as
  702.        #define foo bar ,
  703.    nargs = -1, the `pattern' list is null, and the expansion is just
  704.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  705.    e.g.,
  706.        #define getchar() getc (stdin) .
  707.    When there are args, the expansion is the replacement text with the
  708.    args squashed out, and the reflist is a list describing how to
  709.    build the output from the input: e.g., "3 chars, then the 1st arg,
  710.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  711.    The chars here come from the expansion.  Whatever is left of the
  712.    expansion after the last arg-occurrence is copied after that arg.
  713.    Note that the reflist can be arbitrarily long---
  714.    its length depends on the number of times the arguments appear in
  715.    the replacement text, not how many args there are.  Example:
  716.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  717.    pattern list
  718.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  719.    where (x, y) means (nchars, argno). */
  720.  
  721. typedef struct definition DEFINITION;
  722. struct definition {
  723.   int nargs;
  724.   int length;            /* length of expansion string */
  725.   int predefined;        /* True if the macro was builtin or */
  726.                 /* came from the command line */
  727.   U_CHAR *expansion;
  728.   int line;            /* Line number of definition */
  729.   char *file;            /* File of definition */
  730.   char rest_args;        /* Nonzero if last arg. absorbs the rest */
  731.   struct reflist {
  732.     struct reflist *next;
  733.  
  734.     enum sharp_token_type stringify;    /* set if a # operator before arg */
  735.     enum sharp_token_type raw_before;    /* set if a ## operator before arg */
  736.     enum sharp_token_type raw_after;    /* set if a ## operator after arg */
  737.  
  738.     char rest_args;        /* Nonzero if this arg. absorbs the rest */
  739.     int nchars;            /* Number of literal chars to copy before
  740.                    this arg occurrence.  */
  741.     int argno;            /* Number of arg to substitute (origin-0) */
  742.   } *pattern;
  743.   union {
  744.     /* Names of macro args, concatenated in reverse order
  745.        with comma-space between them.
  746.        The only use of this is that we warn on redefinition
  747.        if this differs between the old and new definitions.  */
  748.     U_CHAR *argnames;
  749.   } args;
  750. };
  751.  
  752. /* different kinds of things that can appear in the value field
  753.    of a hash node.  Actually, this may be useless now. */
  754. union hashval {
  755.   char *cpval;
  756.   DEFINITION *defn;
  757.   KEYDEF *keydef;
  758. };
  759.  
  760. /*
  761.  * special extension string that can be added to the last macro argument to 
  762.  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
  763.  *         #define wow(a, b...)        process (b, a, b)
  764.  *        { wow (1, 2, 3); }    ->    { process (2, 3, 1, 2, 3); }
  765.  *        { wow (one, two); }    ->    { process (two, one, two); }
  766.  * if this "rest_arg" is used with the concat token '##' and if it is not
  767.  * supplied then the token attached to with ## will not be outputted.  Ex:
  768.  *         #define wow (a, b...)        process (b ## , a, ## b)
  769.  *        { wow (1, 2); }        ->    { process (2, 1, 2); }
  770.  *        { wow (one); }        ->    { process (one); {
  771.  */
  772. static char rest_extension[] = "...";
  773. #define REST_EXTENSION_LENGTH    (sizeof (rest_extension) - 1)
  774.  
  775. /* The structure of a node in the hash table.  The hash table
  776.    has entries for all tokens defined by #define directives (type T_MACRO),
  777.    plus some special tokens like __LINE__ (these each have their own
  778.    type, and the appropriate code is run when that type of node is seen.
  779.    It does not contain control words like "#define", which are recognized
  780.    by a separate piece of code. */
  781.  
  782. /* different flavors of hash nodes --- also used in keyword table */
  783. enum node_type {
  784.  T_DEFINE = 1,    /* the `#define' keyword */
  785.  T_INCLUDE,    /* the `#include' keyword */
  786.  T_INCLUDE_NEXT, /* the `#include_next' keyword */
  787.  T_IMPORT,      /* the `#import' keyword */
  788.  T_IFDEF,    /* the `#ifdef' keyword */
  789.  T_IFNDEF,    /* the `#ifndef' keyword */
  790.  T_IF,        /* the `#if' keyword */
  791.  T_ELSE,    /* `#else' */
  792.  T_PRAGMA,    /* `#pragma' */
  793.  T_ELIF,    /* `#elif' */
  794.  T_UNDEF,    /* `#undef' */
  795.  T_LINE,    /* `#line' */
  796.  T_ERROR,    /* `#error' */
  797.  T_WARNING,    /* `#warning' */
  798.  T_ENDIF,    /* `#endif' */
  799.  T_SCCS,    /* `#sccs', used on system V.  */
  800.  T_IDENT,    /* `#ident', used on system V.  */
  801.  T_ASSERT,    /* `#assert', taken from system V.  */
  802.  T_UNASSERT,    /* `#unassert', taken from system V.  */
  803.  T_SPECLINE,    /* special symbol `__LINE__' */
  804.  T_DATE,    /* `__DATE__' */
  805.  T_FILE,    /* `__FILE__' */
  806.  T_BASE_FILE,    /* `__BASE_FILE__' */
  807.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  808.  T_VERSION,    /* `__VERSION__' */
  809.  T_SIZE_TYPE,   /* `__SIZE_TYPE__' */
  810.  T_PTRDIFF_TYPE,   /* `__PTRDIFF_TYPE__' */
  811.  T_WCHAR_TYPE,   /* `__WCHAR_TYPE__' */
  812.  T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
  813.  T_REGISTER_PREFIX_TYPE,   /* `__REGISTER_PREFIX__' */
  814.  T_IMMEDIATE_PREFIX_TYPE,  /* `__IMMEDIATE_PREFIX__' */
  815.  T_TIME,    /* `__TIME__' */
  816.  T_CONST,    /* Constant value, used by `__STDC__' */
  817.  T_MACRO,    /* macro defined by `#define' */
  818.  T_DISABLED,    /* macro temporarily turned off for rescan */
  819.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  820.  T_PCSTRING,    /* precompiled string (hashval is KEYDEF *) */
  821.  T_UNUSED    /* Used for something not defined.  */
  822.  };
  823.  
  824. struct hashnode {
  825.   struct hashnode *next;    /* double links for easy deletion */
  826.   struct hashnode *prev;
  827.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  828.                    chain is kept, in case the node is the head
  829.                    of the chain and gets deleted. */
  830.   enum node_type type;        /* type of special token */
  831.   int length;            /* length of token, for quick comparison */
  832.   U_CHAR *name;            /* the actual name */
  833.   union hashval value;        /* pointer to expansion, or whatever */
  834. };
  835.  
  836. typedef struct hashnode HASHNODE;
  837.  
  838. /* Some definitions for the hash table.  The hash function MUST be
  839.    computed as shown in hashf () below.  That is because the rescan
  840.    loop computes the hash value `on the fly' for most tokens,
  841.    in order to avoid the overhead of a lot of procedure calls to
  842.    the hashf () function.  Hashf () only exists for the sake of
  843.    politeness, for use when speed isn't so important. */
  844.  
  845. #define HASHSIZE 1403
  846. static HASHNODE *hashtab[HASHSIZE];
  847. #define HASHSTEP(old, c) ((old << 2) + c)
  848. #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
  849.  
  850. /* Symbols to predefine.  */
  851.  
  852. #ifdef CPP_PREDEFINES
  853. static char *predefs = CPP_PREDEFINES;
  854. #else
  855. static char *predefs = "";
  856. #endif
  857.  
  858. /* We let tm.h override the types used here, to handle trivial differences
  859.    such as the choice of unsigned int or long unsigned int for size_t.
  860.    When machines start needing nontrivial differences in the size type,
  861.    it would be best to do something here to figure out automatically
  862.    from other information what type to use.  */
  863.  
  864. /* The string value for __SIZE_TYPE__.  */
  865.  
  866. #ifndef SIZE_TYPE
  867. #define SIZE_TYPE "long unsigned int"
  868. #endif
  869.  
  870. /* The string value for __PTRDIFF_TYPE__.  */
  871.  
  872. #ifndef PTRDIFF_TYPE
  873. #define PTRDIFF_TYPE "long int"
  874. #endif
  875.  
  876. /* The string value for __WCHAR_TYPE__.  */
  877.  
  878. #ifndef WCHAR_TYPE
  879. #define WCHAR_TYPE "int"
  880. #endif
  881. char * wchar_type = WCHAR_TYPE;
  882. #undef WCHAR_TYPE
  883.  
  884. /* The string value for __USER_LABEL_PREFIX__ */
  885.  
  886. #ifndef USER_LABEL_PREFIX
  887. #define USER_LABEL_PREFIX ""
  888. #endif
  889.  
  890. /* The string value for __REGISTER_PREFIX__ */
  891.  
  892. #ifndef REGISTER_PREFIX
  893. #define REGISTER_PREFIX ""
  894. #endif
  895.  
  896. /* The string value for __IMMEDIATE_PREFIX__ */
  897.  
  898. #ifndef IMMEDIATE_PREFIX
  899. #define IMMEDIATE_PREFIX ""
  900. #endif
  901.  
  902. /* In the definition of a #assert name, this structure forms
  903.    a list of the individual values asserted.
  904.    Each value is itself a list of "tokens".
  905.    These are strings that are compared by name.  */
  906.  
  907. struct tokenlist_list {
  908.   struct tokenlist_list *next;
  909.   struct arglist *tokens;
  910. };
  911.  
  912. struct assertion_hashnode {
  913.   struct assertion_hashnode *next;    /* double links for easy deletion */
  914.   struct assertion_hashnode *prev;
  915.   /* also, a back pointer to this node's hash
  916.      chain is kept, in case the node is the head
  917.      of the chain and gets deleted. */
  918.   struct assertion_hashnode **bucket_hdr;
  919.   int length;            /* length of token, for quick comparison */
  920.   U_CHAR *name;            /* the actual name */
  921.   /* List of token-sequences.  */
  922.   struct tokenlist_list *value;
  923. };
  924.  
  925. typedef struct assertion_hashnode ASSERTION_HASHNODE;
  926.  
  927. /* Some definitions for the hash table.  The hash function MUST be
  928.    computed as shown in hashf below.  That is because the rescan
  929.    loop computes the hash value `on the fly' for most tokens,
  930.    in order to avoid the overhead of a lot of procedure calls to
  931.    the hashf function.  hashf only exists for the sake of
  932.    politeness, for use when speed isn't so important. */
  933.  
  934. #define ASSERTION_HASHSIZE 37
  935. static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
  936.  
  937. /* Nonzero means inhibit macroexpansion of what seem to be
  938.    assertion tests, in rescan.  For #if.  */
  939. static int assertions_flag;
  940.  
  941. /* `struct directive' defines one #-directive, including how to handle it.  */
  942.  
  943. #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
  944.  
  945. struct directive {
  946.   int length;            /* Length of name */
  947.   int (*func) DO_PROTO;    /* Function to handle directive */
  948.   char *name;            /* Name of directive */
  949.   enum node_type type;        /* Code which describes which directive. */
  950.   char angle_brackets;        /* Nonzero => <...> is special.  */
  951.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  952.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  953. };
  954.  
  955. /* These functions are declared to return int instead of void since they
  956.    are going to be placed in the table and some old compilers have trouble with
  957.    pointers to functions returning void.  */
  958.  
  959. static int do_assert DO_PROTO;
  960. static int do_define DO_PROTO;
  961. static int do_elif DO_PROTO;
  962. static int do_else DO_PROTO;
  963. static int do_endif DO_PROTO;
  964. static int do_error DO_PROTO;
  965. static int do_ident DO_PROTO;
  966. static int do_if DO_PROTO;
  967. static int do_include DO_PROTO;
  968. static int do_line DO_PROTO;
  969. static int do_pragma DO_PROTO;
  970. #ifdef SCCS_DIRECTIVE
  971. static int do_sccs DO_PROTO;
  972. #endif
  973. static int do_unassert DO_PROTO;
  974. static int do_undef DO_PROTO;
  975. static int do_warning DO_PROTO;
  976. static int do_xifdef DO_PROTO;
  977.  
  978. /* Here is the actual list of #-directives, most-often-used first.  */
  979.  
  980. static struct directive directive_table[] = {
  981.   {  6, do_define, "define", T_DEFINE, 0, 1, 1}, /* The last 1 is in future cccp.c added by AMB. */
  982.   {  2, do_if, "if", T_IF},
  983.   {  5, do_xifdef, "ifdef", T_IFDEF},
  984.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  985.   {  5, do_endif, "endif", T_ENDIF},
  986.   {  4, do_else, "else", T_ELSE},
  987.   {  4, do_elif, "elif", T_ELIF},
  988.   {  4, do_line, "line", T_LINE},
  989.   {  7, do_include, "include", T_INCLUDE, 1},
  990.   { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
  991.   {  6, do_include, "import", T_IMPORT, 1},
  992.   {  5, do_undef, "undef", T_UNDEF},
  993.   {  5, do_error, "error", T_ERROR},
  994.   {  7, do_warning, "warning", T_WARNING},
  995. #ifdef SCCS_DIRECTIVE
  996.   {  4, do_sccs, "sccs", T_SCCS},
  997. #endif
  998.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  999.   {  5, do_ident, "ident", T_IDENT},
  1000.   {  6, do_assert, "assert", T_ASSERT},
  1001.   {  8, do_unassert, "unassert", T_UNASSERT},
  1002.   {  -1, 0, "", T_UNUSED},
  1003. };
  1004.  
  1005. /* When a directive handler is called,
  1006.    this points to the # (or the : of the %:) that started the directive.  */
  1007. U_CHAR *directive_start;
  1008.  
  1009. /* table to tell if char can be part of a C identifier. */
  1010. U_CHAR is_idchar[256];
  1011. /* table to tell if char can be first char of a c identifier. */
  1012. U_CHAR is_idstart[256];
  1013. /* table to tell if c is horizontal space.  */
  1014. U_CHAR is_hor_space[256];
  1015. /* table to tell if c is horizontal or vertical space.  */
  1016. static U_CHAR is_space[256];
  1017. /* names of some characters */
  1018. static char *char_name[256];
  1019.  
  1020. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  1021. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  1022.   
  1023. static int errors = 0;            /* Error counter for exit code */
  1024.  
  1025. /* Name of output file, for error messages.  */
  1026. static char *out_fname;
  1027.  
  1028. /* Zero means dollar signs are punctuation.
  1029.    -$ stores 0; -traditional may store 1.  Default is 1 for VMS, 0 otherwise.
  1030.    This must be 0 for correct processing of this ANSI C program:
  1031.     #define foo(a) #a
  1032.     #define lose(b) foo (b)
  1033.     #define test$
  1034.     lose (test)    */
  1035. static int dollars_in_ident;
  1036. #ifndef DOLLARS_IN_IDENTIFIERS
  1037. #define DOLLARS_IN_IDENTIFIERS 1
  1038. #endif
  1039.  
  1040.  
  1041. /* Stack of conditionals currently in progress
  1042.    (including both successful and failing conditionals).  */
  1043.  
  1044. struct if_stack {
  1045.   struct if_stack *next;    /* for chaining to the next stack frame */
  1046.   char *fname;        /* copied from input when frame is made */
  1047.   int lineno;            /* similarly */
  1048.   int if_succeeded;        /* true if a leg of this if-group
  1049.                     has been passed through rescan */
  1050.   U_CHAR *control_macro;    /* For #ifndef at start of file,
  1051.                    this is the macro name tested.  */
  1052.   enum node_type type;        /* type of last directive seen in this group */
  1053. };
  1054. typedef struct if_stack IF_STACK_FRAME;
  1055. static IF_STACK_FRAME *if_stack = NULL;
  1056.  
  1057. /* Buffer of -M output.  */
  1058. static char *deps_buffer;
  1059.  
  1060. /* Number of bytes allocated in above.  */
  1061. static int deps_allocated_size;
  1062.  
  1063. /* Number of bytes used.  */
  1064. static int deps_size;
  1065.  
  1066. /* Number of bytes since the last newline.  */
  1067. static int deps_column;
  1068.  
  1069. /* Nonzero means -I- has been seen,
  1070.    so don't look for #include "foo" the source-file directory.  */
  1071. static int ignore_srcdir;
  1072.  
  1073. static int safe_read PROTO((int, char *, int));
  1074. static void safe_write PROTO((int, char *, int));
  1075.  
  1076. int main PROTO((int, char **));
  1077.  
  1078. static void path_include PROTO((char *));
  1079.  
  1080. static U_CHAR *index0 PROTO((U_CHAR *, int, size_t));
  1081.  
  1082. static void trigraph_pcp PROTO((FILE_BUF *));
  1083.  
  1084. static void newline_fix PROTO((U_CHAR *));
  1085. static void name_newline_fix PROTO((U_CHAR *));
  1086.  
  1087. static char *get_lintcmd PROTO((U_CHAR *, U_CHAR *, U_CHAR **, int *, int *));
  1088.  
  1089. static void rescan PROTO((FILE_BUF *, int));
  1090.  
  1091. static FILE_BUF expand_to_temp_buffer PROTO((U_CHAR *, U_CHAR *, int, int));
  1092.  
  1093. static int handle_directive PROTO((FILE_BUF *, FILE_BUF *));
  1094.  
  1095. static struct tm *timestamp PROTO((void));
  1096. static void special_symbol PROTO((HASHNODE *, FILE_BUF *));
  1097.  
  1098. static int redundant_include_p PROTO((char *));
  1099. static int is_system_include PROTO((char *));
  1100. static char *skip_redundant_dir_prefix PROTO((char *));
  1101.  
  1102. static char *read_filename_string PROTO((int, FILE *));
  1103. static struct file_name_map *read_name_map PROTO((char *));
  1104. static int open_include_file PROTO((char *, struct file_name_list *));
  1105.  
  1106. static void finclude PROTO((int, char *, FILE_BUF *, int, struct file_name_list *));
  1107. static void record_control_macro PROTO((char *, U_CHAR *));
  1108.  
  1109. static int import_hash PROTO((char *));
  1110. static int lookup_import PROTO((char *, struct file_name_list *));
  1111. static void add_import PROTO((int, char *));
  1112.  
  1113. static char *check_precompiled PROTO((int, char *, char **));
  1114. static int check_preconditions PROTO((char *));
  1115. static void pcfinclude PROTO((U_CHAR *, U_CHAR *, U_CHAR *, FILE_BUF *));
  1116. static void pcstring_used PROTO((HASHNODE *));
  1117. static void write_output PROTO((void));
  1118. static void pass_thru_directive PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
  1119.  
  1120. static MACRODEF create_definition PROTO((U_CHAR *, U_CHAR *, FILE_BUF *));
  1121.  
  1122. static int check_macro_name PROTO((U_CHAR *, char *));
  1123. static int compare_defs PROTO((DEFINITION *, DEFINITION *));
  1124. static int comp_def_part PROTO((int, U_CHAR *, int, U_CHAR *, int, int));
  1125.  
  1126. static DEFINITION *collect_expansion  PROTO((U_CHAR *, U_CHAR *, int, struct arglist *));
  1127.  
  1128. int check_assertion PROTO((U_CHAR *, int, int, struct arglist *));
  1129. static int compare_token_lists PROTO((struct arglist *, struct arglist *));
  1130.  
  1131. static struct arglist *read_token_list PROTO((U_CHAR **, U_CHAR *, int *));
  1132. static void free_token_list PROTO((struct arglist *));
  1133.  
  1134. static ASSERTION_HASHNODE *assertion_install PROTO((U_CHAR *, int, int));
  1135. static ASSERTION_HASHNODE *assertion_lookup PROTO((U_CHAR *, int, int));
  1136. static void delete_assertion PROTO((ASSERTION_HASHNODE *));
  1137.  
  1138. static void do_once PROTO((void));
  1139.  
  1140. static HOST_WIDE_INT eval_if_expression PROTO((U_CHAR *, int));
  1141. static void conditional_skip PROTO((FILE_BUF *, int, enum node_type, U_CHAR *, FILE_BUF *));
  1142. static void skip_if_group PROTO((FILE_BUF *, int, FILE_BUF *));
  1143. static void validate_else PROTO((U_CHAR *));
  1144.  
  1145. static U_CHAR *skip_to_end_of_comment PROTO((FILE_BUF *, int *, int));
  1146. static U_CHAR *skip_quoted_string PROTO((U_CHAR *, U_CHAR *, int, int *, int *, int *));
  1147. static char *quote_string PROTO((char *, char *));
  1148. static U_CHAR *skip_paren_group PROTO((FILE_BUF *));
  1149.  
  1150. /* Last arg to output_line_directive.  */
  1151. enum file_change_code {same_file, enter_file, leave_file};
  1152. static void output_line_directive PROTO((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
  1153.  
  1154. static void macroexpand PROTO((HASHNODE *, FILE_BUF *));
  1155.  
  1156. struct argdata;
  1157. static char *macarg PROTO((struct argdata *, int));
  1158.  
  1159. static U_CHAR *macarg1 PROTO((U_CHAR *, U_CHAR *, int *, int *, int *, int));
  1160.  
  1161. static int discard_comments PROTO((U_CHAR *, int, int));
  1162.  
  1163. static int change_newlines PROTO((U_CHAR *, int));
  1164.  
  1165. char *my_strerror PROTO((int));
  1166. void error PRINTF_PROTO_1((char *, ...));
  1167. static void verror PROTO((char *, va_list));
  1168. static void error_from_errno PROTO((char *));
  1169. void warning PRINTF_PROTO_1((char *, ...));
  1170. static void vwarning PROTO((char *, va_list));
  1171. static void error_with_line PRINTF_PROTO_2((int, char *, ...));
  1172. static void verror_with_line PROTO((int, char *, va_list));
  1173. static void vwarning_with_line PROTO((int, char *, va_list));
  1174. static void warning_with_line PRINTF_PROTO_2((int, char *, ...));
  1175. void pedwarn PRINTF_PROTO_1((char *, ...));
  1176. void pedwarn_with_line PRINTF_PROTO_2((int, char *, ...));
  1177. static void pedwarn_with_file_and_line PRINTF_PROTO_3((char *, int, char *, ...));
  1178.  
  1179. static void print_containing_files PROTO((void));
  1180.  
  1181. static int line_for_error PROTO((int));
  1182. static int grow_outbuf PROTO((FILE_BUF *, int));
  1183.  
  1184. static HASHNODE *install PROTO((U_CHAR *, int, enum node_type, char *, int));
  1185. HASHNODE *lookup PROTO((U_CHAR *, int, int));
  1186. static void delete_macro PROTO((HASHNODE *));
  1187. static int hashf PROTO((U_CHAR *, int, int));
  1188.  
  1189. static void dump_single_macro PROTO((HASHNODE *, FILE *));
  1190. static void dump_all_macros PROTO((void));
  1191. static void dump_defn_1 PROTO((U_CHAR *, int, int, FILE *));
  1192. static void dump_arg_n PROTO((DEFINITION *, int, FILE *));
  1193.  
  1194. static void initialize_char_syntax PROTO((void));
  1195. static void initialize_builtins PROTO((FILE_BUF *, FILE_BUF *));
  1196.  
  1197. static void make_definition PROTO((char *, FILE_BUF *));
  1198. static void make_undef PROTO((char *, FILE_BUF *));
  1199.  
  1200. static void make_assertion PROTO((char *, char *));
  1201.  
  1202. static void append_include_chain PROTO((struct file_name_list *, struct file_name_list *));
  1203.  
  1204. static void deps_output PROTO((char *, int));
  1205.  
  1206. static void fatal PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn));
  1207. void fancy_abort PROTO((void)) __attribute__ ((noreturn));
  1208. static void perror_with_name PROTO((char *));
  1209. static void pfatal_with_name PROTO((char *)) __attribute__ ((noreturn));
  1210. static void pipe_closed PROTO((int)) __attribute__ ((noreturn));
  1211.  
  1212. static void memory_full PROTO((void)) __attribute__ ((noreturn));
  1213. GENERIC_PTR xmalloc PROTO((size_t));
  1214. static GENERIC_PTR xrealloc PROTO((GENERIC_PTR, size_t));
  1215. static GENERIC_PTR xcalloc PROTO((size_t, size_t));
  1216. static char *savestring PROTO((char *));
  1217.  
  1218. static int file_size_and_mode PROTO((int, int *, long int *));
  1219.  
  1220. /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
  1221.    retrying if necessary.  Return a negative value if an error occurs,
  1222.    otherwise return the actual number of bytes read,
  1223.    which must be LEN unless end-of-file was reached.  */
  1224.  
  1225. static int
  1226. safe_read (desc, ptr, len)
  1227.      int desc;
  1228.      char *ptr;
  1229.      int len;
  1230. {
  1231.   int left = len;
  1232.   while (left > 0) {
  1233.     int nchars = read (desc, ptr, left);
  1234.     if (nchars < 0)
  1235.       {
  1236. #ifdef EINTR
  1237.     if (errno == EINTR)
  1238.       continue;
  1239. #endif
  1240.     return nchars;
  1241.       }
  1242.     if (nchars == 0)
  1243.       break;
  1244.     ptr += nchars;
  1245.     left -= nchars;
  1246.   }
  1247.   return len - left;
  1248. }
  1249.  
  1250. /* Write LEN bytes at PTR to descriptor DESC,
  1251.    retrying if necessary, and treating any real error as fatal.  */
  1252.  
  1253. static void
  1254. safe_write (desc, ptr, len)
  1255.      int desc;
  1256.      char *ptr;
  1257.      int len;
  1258. {
  1259.   while (len > 0) {
  1260.     int written = write (desc, ptr, len);
  1261.     if (written < 0)
  1262.       {
  1263. #ifdef EINTR
  1264.     if (errno == EINTR)
  1265.       continue;
  1266. #endif
  1267.     pfatal_with_name (out_fname);
  1268.       }
  1269.     ptr += written;
  1270.     len -= written;
  1271.   }
  1272. }
  1273.  
  1274. int
  1275. main (argc, argv)
  1276.      int argc;
  1277.      char **argv;
  1278. {
  1279.   int st_mode;
  1280.   long st_size;
  1281.   char *in_fname;
  1282.   char *cp;
  1283.   int f, i;
  1284.   FILE_BUF *fp;
  1285.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  1286.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  1287.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  1288.   char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
  1289.   char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
  1290.  
  1291.   /* Record the option used with each element of pend_assertions.
  1292.      This is preparation for supporting more than one option for making
  1293.      an assertion.  */
  1294.   char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
  1295.   int inhibit_predefs = 0;
  1296.   int no_standard_includes = 0;
  1297.   int no_standard_cplusplus_includes = 0;
  1298.   int missing_newline = 0;
  1299.  
  1300.   /* Non-0 means don't output the preprocessed program.  */
  1301.   int inhibit_output = 0;
  1302.   /* Non-0 means -v, so print the full set of include dirs.  */
  1303.   int verbose = 0;
  1304.  
  1305.   /* File name which deps are being written to.
  1306.      This is 0 if deps are being written to stdout.  */
  1307.   char *deps_file = 0;
  1308.   /* Fopen file mode to open deps_file with.  */
  1309.   char *deps_mode = "a";
  1310.   /* Stream on which to print the dependency information.  */
  1311.   FILE *deps_stream = 0;
  1312.   /* Target-name to write with the dependency information.  */
  1313.   char *deps_target = 0;
  1314.  
  1315. #ifdef RLIMIT_STACK
  1316.   /* Get rid of any avoidable limit on stack size.  */
  1317.   {
  1318.     struct rlimit rlim;
  1319.  
  1320.     /* Set the stack limit huge so that alloca (particularly stringtab
  1321.      * in dbxread.c) does not fail. */
  1322.     getrlimit (RLIMIT_STACK, &rlim);
  1323.     rlim.rlim_cur = rlim.rlim_max;
  1324.     setrlimit (RLIMIT_STACK, &rlim);
  1325.   }
  1326. #endif /* RLIMIT_STACK defined */
  1327.  
  1328. #ifdef SIGPIPE
  1329.   signal (SIGPIPE, pipe_closed);
  1330. #endif
  1331.  
  1332.   cp = argv[0] + strlen (argv[0]);
  1333.   while (cp != argv[0] && cp[-1] != '/'
  1334. #ifdef DIR_SEPARATOR
  1335.      && cp[-1] != DIR_SEPARATOR
  1336. #endif
  1337.      )
  1338.     --cp;
  1339.   progname = cp;
  1340.  
  1341. #ifdef VMS
  1342.   {
  1343.     /* Remove directories from PROGNAME.  */
  1344.     char *p;
  1345.     char *s = progname;
  1346.  
  1347.     if ((p = rindex (s, ':')) != 0) s = p + 1;    /* skip device */
  1348.     if ((p = rindex (s, ']')) != 0) s = p + 1;    /* skip directory */
  1349.     if ((p = rindex (s, '>')) != 0) s = p + 1;    /* alternate (int'n'l) dir */
  1350.     s = progname = savestring (s);
  1351.     if ((p = rindex (s, ';')) != 0) *p = '\0';    /* strip version number */
  1352.     if ((p = rindex (s, '.')) != 0        /* strip type iff ".exe" */
  1353.     && (p[1] == 'e' || p[1] == 'E')
  1354.     && (p[2] == 'x' || p[2] == 'X')
  1355.     && (p[3] == 'e' || p[3] == 'E')
  1356.     && !p[4])
  1357.       *p = '\0';
  1358.   }
  1359. #endif
  1360.  
  1361.   in_fname = NULL;
  1362.   out_fname = NULL;
  1363.  
  1364.   /* Initialize is_idchar to allow $.  */
  1365.   dollars_in_ident = 1;
  1366.   initialize_char_syntax ();
  1367.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
  1368.  
  1369.   no_line_directives = 0;
  1370.   no_trigraphs = 1;
  1371.   dump_macros = dump_none;
  1372. /* Start new option added by AMB */
  1373.   dump_includes = 0;
  1374. /* End new option added by AMB */
  1375.   no_output = 0;
  1376.   cplusplus = 0;
  1377.   cplusplus_comments = 1;
  1378.  
  1379.   bzero ((char *) pend_files, argc * sizeof (char *));
  1380.   bzero ((char *) pend_defs, argc * sizeof (char *));
  1381.   bzero ((char *) pend_undefs, argc * sizeof (char *));
  1382.   bzero ((char *) pend_assertions, argc * sizeof (char *));
  1383.   bzero ((char *) pend_includes, argc * sizeof (char *));
  1384.  
  1385.   /* Process switches and find input file name.  */
  1386.  
  1387.   for (i = 1; i < argc; i++) {
  1388.     if (argv[i][0] != '-') {
  1389.       if (out_fname != NULL)
  1390.     fatal ("Usage: %s [switches] input output", argv[0]);
  1391.       else if (in_fname != NULL)
  1392.     out_fname = argv[i];
  1393.       else
  1394.     in_fname = argv[i];
  1395.     } else {
  1396.       switch (argv[i][1]) {
  1397.  
  1398.       case 'i':
  1399.     if (!strcmp (argv[i], "-include")) {
  1400.       if (i + 1 == argc)
  1401.         fatal ("Filename missing after `-include' option");
  1402.       else
  1403.         pend_includes[i] = argv[i+1], i++;
  1404.     }
  1405.     if (!strcmp (argv[i], "-imacros")) {
  1406.       if (i + 1 == argc)
  1407.         fatal ("Filename missing after `-imacros' option");
  1408.       else
  1409.         pend_files[i] = argv[i+1], i++;
  1410.     }
  1411.     if (!strcmp (argv[i], "-iprefix")) {
  1412.       if (i + 1 == argc)
  1413.         fatal ("Filename missing after `-iprefix' option");
  1414.       else
  1415.         include_prefix = argv[++i];
  1416.     }
  1417.     if (!strcmp (argv[i], "-ifoutput")) {
  1418.       output_conditionals = 1;
  1419.     }
  1420.     if (!strcmp (argv[i], "-isystem")) {
  1421.       struct file_name_list *dirtmp;
  1422.  
  1423.       if (i + 1 == argc)
  1424.         fatal ("Filename missing after `-isystem' option");
  1425.  
  1426.       dirtmp = (struct file_name_list *)
  1427.         xmalloc (sizeof (struct file_name_list));
  1428.       dirtmp->next = 0;
  1429.       dirtmp->control_macro = 0;
  1430.       dirtmp->c_system_include_path = 1;
  1431.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + 1);
  1432.       strcpy (dirtmp->fname, argv[++i]);
  1433.       dirtmp->got_name_map = 0;
  1434.  
  1435.       if (before_system == 0)
  1436.         before_system = dirtmp;
  1437.       else
  1438.         last_before_system->next = dirtmp;
  1439.       last_before_system = dirtmp; /* Tail follows the last one */
  1440.     }
  1441.     /* Add directory to end of path for includes,
  1442.        with the default prefix at the front of its name.  */
  1443.     if (!strcmp (argv[i], "-iwithprefix")) {
  1444.       struct file_name_list *dirtmp;
  1445.       char *prefix;
  1446.  
  1447.       if (include_prefix != 0)
  1448.         prefix = include_prefix;
  1449.       else {
  1450.         prefix = savestring (GCC_INCLUDE_DIR);
  1451.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1452.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1453.           prefix[strlen (prefix) - 7] = 0;
  1454.       }
  1455.  
  1456.       dirtmp = (struct file_name_list *)
  1457.         xmalloc (sizeof (struct file_name_list));
  1458.       dirtmp->next = 0;    /* New one goes on the end */
  1459.       dirtmp->control_macro = 0;
  1460.       dirtmp->c_system_include_path = 0;
  1461.       if (i + 1 == argc)
  1462.         fatal ("Directory name missing after `-iwithprefix' option");
  1463.  
  1464.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + strlen (prefix) + 1);
  1465.       strcpy (dirtmp->fname, prefix);
  1466.       strcat (dirtmp->fname, argv[++i]);
  1467.       dirtmp->got_name_map = 0;
  1468.  
  1469.       if (after_include == 0)
  1470.         after_include = dirtmp;
  1471.       else
  1472.         last_after_include->next = dirtmp;
  1473.       last_after_include = dirtmp; /* Tail follows the last one */
  1474.     }
  1475.     /* Add directory to main path for includes,
  1476.        with the default prefix at the front of its name.  */
  1477.     if (!strcmp (argv[i], "-iwithprefixbefore")) {
  1478.       struct file_name_list *dirtmp;
  1479.       char *prefix;
  1480.  
  1481.       if (include_prefix != 0)
  1482.         prefix = include_prefix;
  1483.       else {
  1484.         prefix = savestring (GCC_INCLUDE_DIR);
  1485.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1486.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1487.           prefix[strlen (prefix) - 7] = 0;
  1488.       }
  1489.  
  1490.       dirtmp = (struct file_name_list *)
  1491.         xmalloc (sizeof (struct file_name_list));
  1492.       dirtmp->next = 0;    /* New one goes on the end */
  1493.       dirtmp->control_macro = 0;
  1494.       dirtmp->c_system_include_path = 0;
  1495.       if (i + 1 == argc)
  1496.         fatal ("Directory name missing after `-iwithprefixbefore' option");
  1497.  
  1498.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + strlen (prefix) + 1);
  1499.       strcpy (dirtmp->fname, prefix);
  1500.       strcat (dirtmp->fname, argv[++i]);
  1501.       dirtmp->got_name_map = 0;
  1502.  
  1503.       append_include_chain (dirtmp, dirtmp);
  1504.     }
  1505.     /* Add directory to end of path for includes.  */
  1506.     if (!strcmp (argv[i], "-idirafter")) {
  1507.       struct file_name_list *dirtmp;
  1508.  
  1509.       dirtmp = (struct file_name_list *)
  1510.         xmalloc (sizeof (struct file_name_list));
  1511.       dirtmp->next = 0;    /* New one goes on the end */
  1512.       dirtmp->control_macro = 0;
  1513.       dirtmp->c_system_include_path = 0;
  1514.       if (i + 1 == argc)
  1515.         fatal ("Directory name missing after `-idirafter' option");
  1516.       else
  1517.         dirtmp->fname = argv[++i];
  1518.       dirtmp->got_name_map = 0;
  1519.  
  1520.       if (after_include == 0)
  1521.         after_include = dirtmp;
  1522.       else
  1523.         last_after_include->next = dirtmp;
  1524.       last_after_include = dirtmp; /* Tail follows the last one */
  1525.     }
  1526.     break;
  1527.  
  1528.       case 'o':
  1529.     if (out_fname != NULL)
  1530.       fatal ("Output filename specified twice");
  1531.     if (i + 1 == argc)
  1532.       fatal ("Filename missing after -o option");
  1533.     out_fname = argv[++i];
  1534.     if (!strcmp (out_fname, "-"))
  1535.       out_fname = "";
  1536.     break;
  1537.  
  1538.       case 'p':
  1539.     if (!strcmp (argv[i], "-pedantic"))
  1540.       pedantic = 1;
  1541.     else if (!strcmp (argv[i], "-pedantic-errors")) {
  1542.       pedantic = 1;
  1543.       pedantic_errors = 1;
  1544.     } else if (!strcmp (argv[i], "-pcp")) {
  1545.       char *pcp_fname;
  1546.       if (i + 1 == argc)
  1547.         fatal ("Filename missing after -pcp option");
  1548.       pcp_fname = argv[++i];
  1549.       pcp_outfile = 
  1550.         ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
  1551.          ? fopen (pcp_fname, "w")
  1552.          : stdout);
  1553.       if (pcp_outfile == 0)
  1554.         pfatal_with_name (pcp_fname);
  1555.       no_precomp = 1;
  1556.     }
  1557.     break;
  1558.  
  1559.       case 't':
  1560.     if (!strcmp (argv[i], "-traditional")) {
  1561.       traditional = 1;
  1562.       cplusplus_comments = 0;
  1563.       if (dollars_in_ident > 0)
  1564.         dollars_in_ident = 1;
  1565.     } else if (!strcmp (argv[i], "-trigraphs")) {
  1566.       no_trigraphs = 0;
  1567.     }
  1568.     break;
  1569.  
  1570.       case 'l':
  1571.     if (! strcmp (argv[i], "-lang-c"))
  1572.       cplusplus = 0, cplusplus_comments = 1, objc = 0;
  1573.     if (! strcmp (argv[i], "-lang-c89"))
  1574.       cplusplus = 0, cplusplus_comments = 0, objc = 0;
  1575.     if (! strcmp (argv[i], "-lang-c++"))
  1576.       cplusplus = 1, cplusplus_comments = 1, objc = 0;
  1577.     if (! strcmp (argv[i], "-lang-objc"))
  1578.       objc = 1, cplusplus = 0, cplusplus_comments = 1;
  1579.     if (! strcmp (argv[i], "-lang-objc++"))
  1580.       objc = 1, cplusplus = 1, cplusplus_comments = 1;
  1581.      if (! strcmp (argv[i], "-lang-asm"))
  1582.        lang_asm = 1;
  1583.      if (! strcmp (argv[i], "-lint"))
  1584.        for_lint = 1;
  1585.     break;
  1586.  
  1587.       case '+':
  1588.     cplusplus = 1, cplusplus_comments = 1;
  1589.     break;
  1590.  
  1591.       case 'w':
  1592.     inhibit_warnings = 1;
  1593.     break;
  1594.  
  1595.       case 'W':
  1596.     if (!strcmp (argv[i], "-Wtrigraphs"))
  1597.       warn_trigraphs = 1;
  1598.     else if (!strcmp (argv[i], "-Wno-trigraphs"))
  1599.       warn_trigraphs = 0;
  1600.     else if (!strcmp (argv[i], "-Wcomment"))
  1601.       warn_comments = 1;
  1602.     else if (!strcmp (argv[i], "-Wno-comment"))
  1603.       warn_comments = 0;
  1604.     else if (!strcmp (argv[i], "-Wcomments"))
  1605.       warn_comments = 1;
  1606.     else if (!strcmp (argv[i], "-Wno-comments"))
  1607.       warn_comments = 0;
  1608.     else if (!strcmp (argv[i], "-Wtraditional"))
  1609.       warn_stringify = 1;
  1610.     else if (!strcmp (argv[i], "-Wno-traditional"))
  1611.       warn_stringify = 0;
  1612.     else if (!strcmp (argv[i], "-Wimport"))
  1613.       warn_import = 1;
  1614.     else if (!strcmp (argv[i], "-Wno-import"))
  1615.       warn_import = 0;
  1616.     else if (!strcmp (argv[i], "-Werror"))
  1617.       warnings_are_errors = 1;
  1618.     else if (!strcmp (argv[i], "-Wno-error"))
  1619.       warnings_are_errors = 0;
  1620.     else if (!strcmp (argv[i], "-Wall"))
  1621.       {
  1622.         warn_trigraphs = 1;
  1623.         warn_comments = 1;
  1624.       }
  1625.     break;
  1626.  
  1627.       case 'M':
  1628.     /* The style of the choices here is a bit mixed.
  1629.        The chosen scheme is a hybrid of keeping all options in one string
  1630.        and specifying each option in a separate argument:
  1631.        -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
  1632.        -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
  1633.        -M[M][G][D file].  This is awkward to handle in specs, and is not
  1634.        as extensible.  */
  1635.     /* ??? -MG must be specified in addition to one of -M or -MM.
  1636.        This can be relaxed in the future without breaking anything.
  1637.        The converse isn't true.  */
  1638.  
  1639.     /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
  1640.     if (!strcmp (argv[i], "-MG"))
  1641.       {
  1642.         print_deps_missing_files = 1;
  1643.         break;
  1644.       }
  1645.     if (!strcmp (argv[i], "-M"))
  1646.       print_deps = 2;
  1647.     else if (!strcmp (argv[i], "-MM"))
  1648.       print_deps = 1;
  1649.     else if (!strcmp (argv[i], "-MD"))
  1650.       print_deps = 2;
  1651.     else if (!strcmp (argv[i], "-MMD"))
  1652.       print_deps = 1;
  1653.     /* For -MD and -MMD options, write deps on file named by next arg.  */
  1654.     if (!strcmp (argv[i], "-MD")
  1655.         || !strcmp (argv[i], "-MMD")) {
  1656.       if (i + 1 == argc)
  1657.         fatal ("Filename missing after %s option", argv[i]);
  1658.       i++;
  1659.       deps_file = argv[i];
  1660.       deps_mode = "w";
  1661.     } else {
  1662.       /* For -M and -MM, write deps on standard output
  1663.          and suppress the usual output.  */
  1664.       deps_stream = stdout;
  1665.       inhibit_output = 1;
  1666.     }      
  1667.     break;
  1668.  
  1669.       case 'd':
  1670.     {
  1671.       char *p = argv[i] + 2;
  1672.       char c;
  1673.       while ((c = *p++)) {
  1674.         /* Arg to -d specifies what parts of macros to dump */
  1675.         switch (c) {
  1676.         case 'M':
  1677.           dump_macros = dump_only;
  1678.           no_output = 1;
  1679.           break;
  1680.         case 'N':
  1681.           dump_macros = dump_names;
  1682.           break;
  1683.         case 'D':
  1684.           dump_macros = dump_definitions;
  1685.           break;
  1686. /* Start new option added by AMB */
  1687.         case 'I':
  1688.           dump_includes = 1;
  1689.           break;
  1690. /* End new option added by AMB */
  1691.         }
  1692.       }
  1693.     }
  1694.     break;
  1695.  
  1696.       case 'g':
  1697.     if (argv[i][2] == '3')
  1698.       debug_output = 1;
  1699.     break;
  1700.  
  1701.       case 'v':
  1702.     fprintf (stderr, "GNU CPP version %s", version_string);
  1703. #ifdef TARGET_VERSION
  1704.     TARGET_VERSION;
  1705. #endif
  1706.     fprintf (stderr, "\n");
  1707.     verbose = 1;
  1708.     break;
  1709.  
  1710.       case 'H':
  1711.     print_include_names = 1;
  1712.     break;
  1713.  
  1714.       case 'D':
  1715.     if (argv[i][2] != 0)
  1716.       pend_defs[i] = argv[i] + 2;
  1717.     else if (i + 1 == argc)
  1718.       fatal ("Macro name missing after -D option");
  1719.     else
  1720.       i++, pend_defs[i] = argv[i];
  1721.     break;
  1722.  
  1723.       case 'A':
  1724.     {
  1725.       char *p;
  1726.  
  1727.       if (argv[i][2] != 0)
  1728.         p = argv[i] + 2;
  1729.       else if (i + 1 == argc)
  1730.         fatal ("Assertion missing after -A option");
  1731.       else
  1732.         p = argv[++i];
  1733.  
  1734.       if (!strcmp (p, "-")) {
  1735.         /* -A- eliminates all predefined macros and assertions.
  1736.            Let's include also any that were specified earlier
  1737.            on the command line.  That way we can get rid of any
  1738.            that were passed automatically in from GCC.  */
  1739.         int j;
  1740.         inhibit_predefs = 1;
  1741.         for (j = 0; j < i; j++)
  1742.           pend_defs[j] = pend_assertions[j] = 0;
  1743.       } else {
  1744.         pend_assertions[i] = p;
  1745.         pend_assertion_options[i] = "-A";
  1746.       }
  1747.     }
  1748.     break;
  1749.  
  1750.       case 'U':        /* JF #undef something */
  1751.     if (argv[i][2] != 0)
  1752.       pend_undefs[i] = argv[i] + 2;
  1753.     else if (i + 1 == argc)
  1754.       fatal ("Macro name missing after -U option");
  1755.     else
  1756.       pend_undefs[i] = argv[i+1], i++;
  1757.     break;
  1758.  
  1759.       case 'C':
  1760.     put_out_comments = 1;
  1761.     break;
  1762.  
  1763.       case 'E':            /* -E comes from cc -E; ignore it.  */
  1764.     break;
  1765.  
  1766.       case 'P':
  1767.     no_line_directives = 1;
  1768.     break;
  1769.  
  1770.       case '$':            /* Don't include $ in identifiers.  */
  1771.     dollars_in_ident = 0;
  1772.     break;
  1773.  
  1774.       case 'I':            /* Add directory to path for includes.  */
  1775.     {
  1776.       struct file_name_list *dirtmp;
  1777.  
  1778.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1779.         ignore_srcdir = 1;
  1780.         /* Don't use any preceding -I directories for #include <...>.  */
  1781.         first_bracket_include = 0;
  1782.       }
  1783.       else {
  1784.         dirtmp = (struct file_name_list *)
  1785.           xmalloc (sizeof (struct file_name_list));
  1786.         dirtmp->next = 0;        /* New one goes on the end */
  1787.         dirtmp->control_macro = 0;
  1788.         dirtmp->c_system_include_path = 0;
  1789.         if (argv[i][2] != 0)
  1790.           dirtmp->fname = argv[i] + 2;
  1791.         else if (i + 1 == argc)
  1792.           fatal ("Directory name missing after -I option");
  1793.         else
  1794.           dirtmp->fname = argv[++i];
  1795.         dirtmp->got_name_map = 0;
  1796.         append_include_chain (dirtmp, dirtmp);
  1797.       }
  1798.     }
  1799.     break;
  1800.  
  1801.       case 'n':
  1802.     if (!strcmp (argv[i], "-nostdinc"))
  1803.       /* -nostdinc causes no default include directories.
  1804.          You must specify all include-file directories with -I.  */
  1805.       no_standard_includes = 1;
  1806.     else if (!strcmp (argv[i], "-nostdinc++"))
  1807.       /* -nostdinc++ causes no default C++-specific include directories. */
  1808.       no_standard_cplusplus_includes = 1;
  1809.     else if (!strcmp (argv[i], "-noprecomp"))
  1810.       no_precomp = 1;
  1811.     break;
  1812.  
  1813.       case 'u':
  1814.     /* Sun compiler passes undocumented switch "-undef".
  1815.        Let's assume it means to inhibit the predefined symbols.  */
  1816.     inhibit_predefs = 1;
  1817.     break;
  1818.  
  1819.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  1820.     if (in_fname == NULL) {
  1821.       in_fname = "";
  1822.       break;
  1823.     } else if (out_fname == NULL) {
  1824.       out_fname = "";
  1825.       break;
  1826.     }    /* else fall through into error */
  1827.  
  1828.       default:
  1829.     fatal ("Invalid option `%s'", argv[i]);
  1830.       }
  1831.     }
  1832.   }
  1833.  
  1834.   /* Add dirs from CPATH after dirs from -I.  */
  1835.   /* There seems to be confusion about what CPATH should do,
  1836.      so for the moment it is not documented.  */
  1837.   /* Some people say that CPATH should replace the standard include dirs,
  1838.      but that seems pointless: it comes before them, so it overrides them
  1839.      anyway.  */
  1840.   cp = getenv ("CPATH");
  1841.   if (cp && ! no_standard_includes)
  1842.     path_include (cp);
  1843.  
  1844.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  1845.   initialize_char_syntax ();
  1846.  
  1847.   /* Initialize output buffer */
  1848.  
  1849.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  1850.   outbuf.bufp = outbuf.buf;
  1851.   outbuf.length = OUTBUF_SIZE;
  1852.  
  1853.   /* Do partial setup of input buffer for the sake of generating
  1854.      early #line directives (when -g is in effect).  */
  1855.  
  1856.   fp = &instack[++indepth];
  1857.   if (in_fname == NULL)
  1858.     in_fname = "";
  1859.   fp->nominal_fname = fp->fname = in_fname;
  1860.   fp->lineno = 0;
  1861.  
  1862.   /* In C++, wchar_t is a distinct basic type, and we can expect
  1863.      __wchar_t to be defined by cc1plus.  */
  1864.   if (cplusplus)
  1865.     wchar_type = "__wchar_t";
  1866.  
  1867.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  1868.      and option processing.  */
  1869.   initialize_builtins (fp, &outbuf);
  1870.  
  1871.   /* Do standard #defines and assertions
  1872.      that identify system and machine type.  */
  1873.  
  1874.   if (!inhibit_predefs) {
  1875.     char *p = (char *) alloca (strlen (predefs) + 1);
  1876.     strcpy (p, predefs);
  1877.     while (*p) {
  1878.       char *q;
  1879.       while (*p == ' ' || *p == '\t')
  1880.     p++;
  1881.       /* Handle -D options.  */ 
  1882.       if (p[0] == '-' && p[1] == 'D') {
  1883.     q = &p[2];
  1884.     while (*p && *p != ' ' && *p != '\t')
  1885.       p++;
  1886.     if (*p != 0)
  1887.       *p++= 0;
  1888.     if (debug_output)
  1889.       output_line_directive (fp, &outbuf, 0, same_file);
  1890.     make_definition (q, &outbuf);
  1891.     while (*p == ' ' || *p == '\t')
  1892.       p++;
  1893.       } else if (p[0] == '-' && p[1] == 'A') {
  1894.     /* Handle -A options (assertions).  */ 
  1895.     char *assertion;
  1896.     char *past_name;
  1897.     char *value;
  1898.     char *past_value;
  1899.     char *termination;
  1900.     int save_char;
  1901.  
  1902.     assertion = &p[2];
  1903.     past_name = assertion;
  1904.     /* Locate end of name.  */
  1905.     while (*past_name && *past_name != ' '
  1906.            && *past_name != '\t' && *past_name != '(')
  1907.       past_name++;
  1908.     /* Locate `(' at start of value.  */
  1909.     value = past_name;
  1910.     while (*value && (*value == ' ' || *value == '\t'))
  1911.       value++;
  1912.     if (*value++ != '(')
  1913.       abort ();
  1914.     while (*value && (*value == ' ' || *value == '\t'))
  1915.       value++;
  1916.     past_value = value;
  1917.     /* Locate end of value.  */
  1918.     while (*past_value && *past_value != ' '
  1919.            && *past_value != '\t' && *past_value != ')')
  1920.       past_value++;
  1921.     termination = past_value;
  1922.     while (*termination && (*termination == ' ' || *termination == '\t'))
  1923.       termination++;
  1924.     if (*termination++ != ')')
  1925.       abort ();
  1926.     if (*termination && *termination != ' ' && *termination != '\t')
  1927.       abort ();
  1928.     /* Temporarily null-terminate the value.  */
  1929.     save_char = *termination;
  1930.     *termination = '\0';
  1931.     /* Install the assertion.  */
  1932.     make_assertion ("-A", assertion);
  1933.     *termination = (char) save_char;
  1934.     p = termination;
  1935.     while (*p == ' ' || *p == '\t')
  1936.       p++;
  1937.       } else {
  1938.     abort ();
  1939.       }
  1940.     }
  1941.   }
  1942.  
  1943.   /* Now handle the command line options.  */
  1944.  
  1945.   /* Do -U's, -D's and -A's in the order they were seen.  */
  1946.   for (i = 1; i < argc; i++) {
  1947.     if (pend_undefs[i]) {
  1948.       if (debug_output)
  1949.         output_line_directive (fp, &outbuf, 0, same_file);
  1950.       make_undef (pend_undefs[i], &outbuf);
  1951.     }
  1952.     if (pend_defs[i]) {
  1953.       if (debug_output)
  1954.         output_line_directive (fp, &outbuf, 0, same_file);
  1955.       make_definition (pend_defs[i], &outbuf);
  1956.     }
  1957.     if (pend_assertions[i])
  1958.       make_assertion (pend_assertion_options[i], pend_assertions[i]);
  1959.   }
  1960.  
  1961.   done_initializing = 1;
  1962.  
  1963.   { /* read the appropriate environment variable and if it exists
  1964.        replace include_defaults with the listed path. */
  1965.     char *epath = 0;
  1966.     switch ((objc << 1) + cplusplus)
  1967.       {
  1968.       case 0:
  1969.     epath = getenv ("C_INCLUDE_PATH");
  1970.     break;
  1971.       case 1:
  1972.     epath = getenv ("CPLUS_INCLUDE_PATH");
  1973.     break;
  1974.       case 2:
  1975.     epath = getenv ("OBJC_INCLUDE_PATH");
  1976.     break;
  1977.       case 3:
  1978.     epath = getenv ("OBJCPLUS_INCLUDE_PATH");
  1979.     break;
  1980.       }
  1981.     /* If the environment var for this language is set,
  1982.        add to the default list of include directories.  */
  1983.     if (epath) {
  1984.       char *nstore = (char *) alloca (strlen (epath) + 2);
  1985.       int num_dirs;
  1986.       char *startp, *endp;
  1987.  
  1988.       for (num_dirs = 1, startp = epath; *startp; startp++)
  1989.     if (*startp == PATH_SEPARATOR)
  1990.       num_dirs++;
  1991.       include_defaults
  1992.     = (struct default_include *) xmalloc ((num_dirs
  1993.                            * sizeof (struct default_include))
  1994.                           + sizeof (include_defaults_array));
  1995.       startp = endp = epath;
  1996.       num_dirs = 0;
  1997.       while (1) {
  1998.         /* Handle cases like c:/usr/lib:d:/gcc/lib */
  1999.         if ((*endp == PATH_SEPARATOR
  2000. #if 0 /* Obsolete, now that we use semicolons as the path separator.  */
  2001. #ifdef __MSDOS__
  2002.          && (endp-startp != 1 || !isalpha (*startp))
  2003. #endif
  2004. #endif
  2005.          )
  2006.             || *endp == 0) {
  2007.       strncpy (nstore, startp, endp-startp);
  2008.       if (endp == startp)
  2009.         strcpy (nstore, ".");
  2010.       else
  2011.         nstore[endp-startp] = '\0';
  2012.  
  2013.       include_defaults[num_dirs].fname = savestring (nstore);
  2014.       include_defaults[num_dirs].cplusplus = cplusplus;
  2015.       include_defaults[num_dirs].cxx_aware = 1;
  2016.       num_dirs++;
  2017.       if (*endp == '\0')
  2018.         break;
  2019.       endp = startp = endp + 1;
  2020.     } else
  2021.       endp++;
  2022.       }
  2023.       /* Put the usual defaults back in at the end.  */
  2024.       bcopy ((char *) include_defaults_array,
  2025.          (char *) &include_defaults[num_dirs],
  2026.          sizeof (include_defaults_array));
  2027.     }
  2028.   }
  2029.  
  2030.   append_include_chain (before_system, last_before_system);
  2031.   first_system_include = before_system;
  2032.  
  2033.   /* Unless -fnostdinc,
  2034.      tack on the standard include file dirs to the specified list */
  2035.   if (!no_standard_includes) {
  2036.     struct default_include *p = include_defaults;
  2037.     char *specd_prefix = include_prefix;
  2038.     char *default_prefix = savestring (GCC_INCLUDE_DIR);
  2039.     int default_len = 0;
  2040.     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  2041.     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
  2042.       default_len = strlen (default_prefix) - 7;
  2043.       default_prefix[default_len] = 0;
  2044.     }
  2045.     /* Search "translated" versions of GNU directories.
  2046.        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
  2047.     if (specd_prefix != 0 && default_len != 0)
  2048.       for (p = include_defaults; p->fname; p++) {
  2049.     /* Some standard dirs are only for C++.  */
  2050.     if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2051.       /* Does this dir start with the prefix?  */
  2052.       if (!strncmp (p->fname, default_prefix, default_len)) {
  2053.         /* Yes; change prefix and add to search list.  */
  2054.         struct file_name_list *new
  2055.           = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2056.         int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
  2057.         char *str = xmalloc (this_len + 1);
  2058.         strcpy (str, specd_prefix);
  2059.         strcat (str, p->fname + default_len);
  2060.         new->fname = str;
  2061.         new->control_macro = 0;
  2062.         new->c_system_include_path = !p->cxx_aware;
  2063.         new->got_name_map = 0;
  2064.         append_include_chain (new, new);
  2065.         if (first_system_include == 0)
  2066.           first_system_include = new;
  2067.       }
  2068.     }
  2069.       }
  2070.     /* Search ordinary names for GNU include directories.  */
  2071.     for (p = include_defaults; p->fname; p++) {
  2072.       /* Some standard dirs are only for C++.  */
  2073.       if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2074.     struct file_name_list *new
  2075.       = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2076.     new->control_macro = 0;
  2077.     new->c_system_include_path = !p->cxx_aware;
  2078.     new->fname = p->fname;
  2079.     new->got_name_map = 0;
  2080.     append_include_chain (new, new);
  2081.     if (first_system_include == 0)
  2082.       first_system_include = new;
  2083.       }
  2084.     }
  2085.   }
  2086.  
  2087.   /* Tack the after_include chain at the end of the include chain.  */
  2088.   append_include_chain (after_include, last_after_include);
  2089.   if (first_system_include == 0)
  2090.     first_system_include = after_include;
  2091.  
  2092.   /* With -v, print the list of dirs to search.  */
  2093.   if (verbose) {
  2094.     struct file_name_list *p;
  2095.     fprintf (stderr, "#include \"...\" search starts here:\n");
  2096.     for (p = include; p; p = p->next) {
  2097.       if (p == first_bracket_include)
  2098.     fprintf (stderr, "#include <...> search starts here:\n");
  2099.       fprintf (stderr, " %s\n", p->fname);
  2100.     }
  2101.     fprintf (stderr, "End of search list.\n");
  2102.   }
  2103.  
  2104.   /* Scan the -imacros files before the main input.
  2105.      Much like #including them, but with no_output set
  2106.      so that only their macro definitions matter.  */
  2107.  
  2108.   no_output++; no_record_file++;
  2109.   for (i = 1; i < argc; i++)
  2110.     if (pend_files[i]) {
  2111.       int fd = open (pend_files[i], O_RDONLY, 0666);
  2112.       if (fd < 0) {
  2113.     perror_with_name (pend_files[i]);
  2114.     return FATAL_EXIT_CODE;
  2115.       }
  2116.       finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
  2117.     }
  2118.   no_output--; no_record_file--;
  2119.  
  2120.   /* Copy the entire contents of the main input file into
  2121.      the stacked input buffer previously allocated for it.  */
  2122.  
  2123.   /* JF check for stdin */
  2124.   if (in_fname == NULL || *in_fname == 0) {
  2125.     in_fname = "";
  2126.     f = 0;
  2127.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  2128.     goto perror;
  2129.  
  2130.   /* -MG doesn't select the form of output and must be specified with one of
  2131.      -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
  2132.      inhibit compilation.  */
  2133.   if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
  2134.     fatal ("-MG must be specified with one of -M or -MM");
  2135.  
  2136.   /* Either of two environment variables can specify output of deps.
  2137.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  2138.      where OUTPUT_FILE is the file to write deps info to
  2139.      and DEPS_TARGET is the target to mention in the deps.  */
  2140.  
  2141.   if (print_deps == 0
  2142.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  2143.       || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
  2144.     char *spec = getenv ("DEPENDENCIES_OUTPUT");
  2145.     char *s;
  2146.     char *output_file;
  2147.  
  2148.     if (spec == 0) {
  2149.       spec = getenv ("SUNPRO_DEPENDENCIES");
  2150.       print_deps = 2;
  2151.     }
  2152.     else
  2153.       print_deps = 1;
  2154.  
  2155.     s = spec;
  2156.     /* Find the space before the DEPS_TARGET, if there is one.  */
  2157.     /* This should use index.  (mrs) */
  2158.     while (*s != 0 && *s != ' ') s++;
  2159.     if (*s != 0) {
  2160.       deps_target = s + 1;
  2161.       output_file = xmalloc (s - spec + 1);
  2162.       bcopy (spec, output_file, s - spec);
  2163.       output_file[s - spec] = 0;
  2164.     }
  2165.     else {
  2166.       deps_target = 0;
  2167.       output_file = spec;
  2168.     }
  2169.       
  2170.     deps_file = output_file;
  2171.     deps_mode = "a";
  2172.   }
  2173.  
  2174.   /* For -M, print the expected object file name
  2175.      as the target of this Make-rule.  */
  2176.   if (print_deps) {
  2177.     deps_allocated_size = 200;
  2178.     deps_buffer = xmalloc (deps_allocated_size);
  2179.     deps_buffer[0] = 0;
  2180.     deps_size = 0;
  2181.     deps_column = 0;
  2182.  
  2183.     if (deps_target) {
  2184.       deps_output (deps_target, ':');
  2185.     } else if (*in_fname == 0) {
  2186.       deps_output ("-", ':');
  2187.     } else {
  2188.       char *p, *q;
  2189.       int len;
  2190.  
  2191.       /* Discard all directory prefixes from filename.  */
  2192.       if ((q = rindex (in_fname, '/')) != NULL
  2193. #ifdef DIR_SEPARATOR
  2194.       && (q = rindex (in_fname, DIR_SEPARATOR)) != NULL
  2195. #endif
  2196.       )
  2197.     ++q;
  2198.       else
  2199.     q = in_fname;
  2200.  
  2201.       /* Copy remainder to mungable area.  */
  2202.       p = (char *) alloca (strlen(q) + 8);
  2203.       strcpy (p, q);
  2204.  
  2205.       /* Output P, but remove known suffixes.  */
  2206.       len = strlen (p);
  2207.       q = p + len;
  2208.       if (len >= 2
  2209.       && p[len - 2] == '.'
  2210.       && index("cCsSm", p[len - 1]))
  2211.     q = p + (len - 2);
  2212.       else if (len >= 3
  2213.            && p[len - 3] == '.'
  2214.            && p[len - 2] == 'c'
  2215.            && p[len - 1] == 'c')
  2216.     q = p + (len - 3);
  2217.       else if (len >= 4
  2218.            && p[len - 4] == '.'
  2219.            && p[len - 3] == 'c'
  2220.            && p[len - 2] == 'x'
  2221.            && p[len - 1] == 'x')
  2222.     q = p + (len - 4);
  2223.       else if (len >= 4
  2224.            && p[len - 4] == '.'
  2225.            && p[len - 3] == 'c'
  2226.            && p[len - 2] == 'p'
  2227.            && p[len - 1] == 'p')
  2228.     q = p + (len - 4);
  2229.  
  2230.       /* Supply our own suffix.  */
  2231. #ifndef VMS
  2232.       strcpy (q, ".o");
  2233. #else
  2234.       strcpy (q, ".obj");
  2235. #endif
  2236.  
  2237.       deps_output (p, ':');
  2238.       deps_output (in_fname, ' ');
  2239.     }
  2240.   }
  2241.  
  2242.   file_size_and_mode (f, &st_mode, &st_size);
  2243.   fp->nominal_fname = fp->fname = in_fname;
  2244.   fp->lineno = 1;
  2245.   fp->system_header_p = 0;
  2246.   /* JF all this is mine about reading pipes and ttys */
  2247.   if (! S_ISREG (st_mode)) {
  2248.     /* Read input from a file that is not a normal disk file.
  2249.        We cannot preallocate a buffer with the correct size,
  2250.        so we must read in the file a piece at the time and make it bigger.  */
  2251.     int size;
  2252.     int bsize;
  2253.     int cnt;
  2254.  
  2255.     bsize = 2000;
  2256.     size = 0;
  2257.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  2258.     for (;;) {
  2259.       cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
  2260.       if (cnt < 0) goto perror;    /* error! */
  2261.       size += cnt;
  2262.       if (size != bsize) break;    /* End of file */
  2263.       bsize *= 2;
  2264.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  2265.     }
  2266.     fp->length = size;
  2267.   } else {
  2268.     /* Read a file whose size we can determine in advance.
  2269.        For the sake of VMS, st_size is just an upper bound.  */
  2270.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  2271.     fp->length = safe_read (f, (char *) fp->buf, st_size);
  2272.     if (fp->length < 0) goto perror;
  2273.   }
  2274.   fp->bufp = fp->buf;
  2275.   fp->if_stack = if_stack;
  2276.  
  2277.   /* Make sure data ends with a newline.  And put a null after it.  */
  2278.  
  2279.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  2280.       /* Backslash-newline at end is not good enough.  */
  2281.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  2282.     fp->buf[fp->length++] = '\n';
  2283.     missing_newline = 1;
  2284.   }
  2285.   fp->buf[fp->length] = '\0';
  2286.  
  2287.   /* Unless inhibited, convert trigraphs in the input.  */
  2288.  
  2289.   if (!no_trigraphs)
  2290.     trigraph_pcp (fp);
  2291.  
  2292.   /* Now that we know the input file is valid, open the output.  */
  2293.  
  2294.   if (!out_fname || !strcmp (out_fname, ""))
  2295.     out_fname = "stdout";
  2296.   else if (! freopen (out_fname, "w", stdout))
  2297.     pfatal_with_name (out_fname);
  2298.  
  2299.   output_line_directive (fp, &outbuf, 0, same_file);
  2300.  
  2301.   /* Scan the -include files before the main input.  */
  2302.  
  2303.   no_record_file++;
  2304.   for (i = 1; i < argc; i++)
  2305.     if (pend_includes[i]) {
  2306.       int fd = open (pend_includes[i], O_RDONLY, 0666);
  2307.       if (fd < 0) {
  2308.     perror_with_name (pend_includes[i]);
  2309.     return FATAL_EXIT_CODE;
  2310.       }
  2311.       finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
  2312.     }
  2313.   no_record_file--;
  2314.  
  2315.   /* Scan the input, processing macros and directives.  */
  2316.  
  2317.   rescan (&outbuf, 0);
  2318.  
  2319.   if (missing_newline)
  2320.     fp->lineno--;
  2321.  
  2322.   if (pedantic && missing_newline)
  2323.     pedwarn ("file does not end in newline");
  2324.  
  2325.   /* Now we have processed the entire input
  2326.      Write whichever kind of output has been requested.  */
  2327.  
  2328.   if (dump_macros == dump_only)
  2329.     dump_all_macros ();
  2330.   else if (! inhibit_output) {
  2331.     write_output ();
  2332.   }
  2333.  
  2334.   if (print_deps) {
  2335.     /* Don't actually write the deps file if compilation has failed.  */
  2336.     if (errors == 0) {
  2337.       if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
  2338.     pfatal_with_name (deps_file);
  2339.       fputs (deps_buffer, deps_stream);
  2340.       putc ('\n', deps_stream);
  2341.       if (deps_file) {
  2342.     if (ferror (deps_stream) || fclose (deps_stream) != 0)
  2343.       fatal ("I/O error on output");
  2344.       }
  2345.     }
  2346.   }
  2347.  
  2348.   if (pcp_outfile && pcp_outfile != stdout
  2349.       && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
  2350.     fatal ("I/O error on `-pcp' output");
  2351.  
  2352.   if (ferror (stdout) || fclose (stdout) != 0)
  2353.     fatal ("I/O error on output");
  2354.  
  2355.   if (errors)
  2356.     exit (FATAL_EXIT_CODE);
  2357.   exit (SUCCESS_EXIT_CODE);
  2358.  
  2359.  perror:
  2360.   pfatal_with_name (in_fname);
  2361.   return 0;
  2362. }
  2363.  
  2364. /* Given a colon-separated list of file names PATH,
  2365.    add all the names to the search path for include files.  */
  2366.  
  2367. static void
  2368. path_include (path)
  2369.      char *path;
  2370. {
  2371.   char *p;
  2372.  
  2373.   p = path;
  2374.  
  2375.   if (*p)
  2376.     while (1) {
  2377.       char *q = p;
  2378.       char *name;
  2379.       struct file_name_list *dirtmp;
  2380.  
  2381.       /* Find the end of this name.  */
  2382.       while (*q != 0 && *q != PATH_SEPARATOR) q++;
  2383.       if (p == q) {
  2384.     /* An empty name in the path stands for the current directory.  */
  2385.     name = xmalloc (2);
  2386.     name[0] = '.';
  2387.     name[1] = 0;
  2388.       } else {
  2389.     /* Otherwise use the directory that is named.  */
  2390.     name = xmalloc (q - p + 1);
  2391.     bcopy (p, name, q - p);
  2392.     name[q - p] = 0;
  2393.       }
  2394.  
  2395.       dirtmp = (struct file_name_list *)
  2396.     xmalloc (sizeof (struct file_name_list));
  2397.       dirtmp->next = 0;        /* New one goes on the end */
  2398.       dirtmp->control_macro = 0;
  2399.       dirtmp->c_system_include_path = 0;
  2400.       dirtmp->fname = name;
  2401.       dirtmp->got_name_map = 0;
  2402.       append_include_chain (dirtmp, dirtmp);
  2403.  
  2404.       /* Advance past this name.  */
  2405.       p = q;
  2406.       if (*p == 0)
  2407.     break;
  2408.       /* Skip the colon.  */
  2409.       p++;
  2410.     }
  2411. }
  2412.  
  2413. /* Return the address of the first character in S that equals C.
  2414.    S is an array of length N, possibly containing '\0's, and followed by '\0'.
  2415.    Return 0 if there is no such character.  Assume that C itself is not '\0'.
  2416.    If we knew we could use memchr, we could just invoke memchr (S, C, N),
  2417.    but unfortunately memchr isn't autoconfigured yet.  */
  2418.  
  2419. static U_CHAR *
  2420. index0 (s, c, n)
  2421.      U_CHAR *s;
  2422.      int c;
  2423.      size_t n;
  2424. {
  2425.   char *p = (char *) s;
  2426.   for (;;) {
  2427.     char *q = index (p, c);
  2428.     if (q)
  2429.       return (U_CHAR *) q;
  2430.     else {
  2431.       size_t l = strlen (p);
  2432.       if (l == n)
  2433.     return 0;
  2434.       l++;
  2435.       p += l;
  2436.       n -= l;
  2437.     }
  2438.   }
  2439. }
  2440.  
  2441. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  2442.    before main CCCP processing.  Name `pcp' is also in honor of the
  2443.    drugs the trigraph designers must have been on.
  2444.  
  2445.    Using an extra pass through the buffer takes a little extra time,
  2446.    but is infinitely less hairy than trying to handle trigraphs inside
  2447.    strings, etc. everywhere, and also makes sure that trigraphs are
  2448.    only translated in the top level of processing. */
  2449.  
  2450. static void
  2451. trigraph_pcp (buf)
  2452.      FILE_BUF *buf;
  2453. {
  2454.   register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
  2455.   int len;
  2456.  
  2457.   fptr = bptr = sptr = buf->buf;
  2458.   lptr = fptr + buf->length;
  2459.   while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
  2460.     if (*++sptr != '?')
  2461.       continue;
  2462.     switch (*++sptr) {
  2463.       case '=':
  2464.       c = '#';
  2465.       break;
  2466.     case '(':
  2467.       c = '[';
  2468.       break;
  2469.     case '/':
  2470.       c = '\\';
  2471.       break;
  2472.     case ')':
  2473.       c = ']';
  2474.       break;
  2475.     case '\'':
  2476.       c = '^';
  2477.       break;
  2478.     case '<':
  2479.       c = '{';
  2480.       break;
  2481.     case '!':
  2482.       c = '|';
  2483.       break;
  2484.     case '>':
  2485.       c = '}';
  2486.       break;
  2487.     case '-':
  2488.       c  = '~';
  2489.       break;
  2490.     case '?':
  2491.       sptr--;
  2492.       continue;
  2493.     default:
  2494.       continue;
  2495.     }
  2496.     len = sptr - fptr - 2;
  2497.  
  2498.     /* BSD doc says bcopy () works right for overlapping strings.  In ANSI
  2499.        C, this will be memmove (). */
  2500.     if (bptr != fptr && len > 0)
  2501.       bcopy ((char *) fptr, (char *) bptr, len);
  2502.  
  2503.     bptr += len;
  2504.     *bptr++ = c;
  2505.     fptr = ++sptr;
  2506.   }
  2507.   len = buf->length - (fptr - buf->buf);
  2508.   if (bptr != fptr && len > 0)
  2509.     bcopy ((char *) fptr, (char *) bptr, len);
  2510.   buf->length -= fptr - bptr;
  2511.   buf->buf[buf->length] = '\0';
  2512.   if (warn_trigraphs && fptr != bptr)
  2513.     warning_with_line (0, "%d trigraph(s) encountered", (fptr - bptr) / 2);
  2514. }
  2515.  
  2516. /* Move all backslash-newline pairs out of embarrassing places.
  2517.    Exchange all such pairs following BP
  2518.    with any potentially-embarrassing characters that follow them.
  2519.    Potentially-embarrassing characters are / and *
  2520.    (because a backslash-newline inside a comment delimiter
  2521.    would cause it not to be recognized).  */
  2522.  
  2523. static void
  2524. newline_fix (bp)
  2525.      U_CHAR *bp;
  2526. {
  2527.   register U_CHAR *p = bp;
  2528.  
  2529.   /* First count the backslash-newline pairs here.  */
  2530.  
  2531.   while (p[0] == '\\' && p[1] == '\n')
  2532.     p += 2;
  2533.  
  2534.   /* What follows the backslash-newlines is not embarrassing.  */
  2535.  
  2536.   if (*p != '/' && *p != '*')
  2537.     return;
  2538.  
  2539.   /* Copy all potentially embarrassing characters
  2540.      that follow the backslash-newline pairs
  2541.      down to where the pairs originally started.  */
  2542.  
  2543.   while (*p == '*' || *p == '/')
  2544.     *bp++ = *p++;
  2545.  
  2546.   /* Now write the same number of pairs after the embarrassing chars.  */
  2547.   while (bp < p) {
  2548.     *bp++ = '\\';
  2549.     *bp++ = '\n';
  2550.   }
  2551. }
  2552.  
  2553. /* Like newline_fix but for use within a directive-name.
  2554.    Move any backslash-newlines up past any following symbol constituents.  */
  2555.  
  2556. static void
  2557. name_newline_fix (bp)
  2558.      U_CHAR *bp;
  2559. {
  2560.   register U_CHAR *p = bp;
  2561.  
  2562.   /* First count the backslash-newline pairs here.  */
  2563.   while (p[0] == '\\' && p[1] == '\n')
  2564.     p += 2;
  2565.  
  2566.   /* What follows the backslash-newlines is not embarrassing.  */
  2567.  
  2568.   if (!is_idchar[*p])
  2569.     return;
  2570.  
  2571.   /* Copy all potentially embarrassing characters
  2572.      that follow the backslash-newline pairs
  2573.      down to where the pairs originally started.  */
  2574.  
  2575.   while (is_idchar[*p])
  2576.     *bp++ = *p++;
  2577.  
  2578.   /* Now write the same number of pairs after the embarrassing chars.  */
  2579.   while (bp < p) {
  2580.     *bp++ = '\\';
  2581.     *bp++ = '\n';
  2582.   }
  2583. }
  2584.  
  2585. /* Look for lint commands in comments.
  2586.  
  2587.    When we come in here, ibp points into a comment.  Limit is as one expects.
  2588.    scan within the comment -- it should start, after lwsp, with a lint command.
  2589.    If so that command is returned as a (constant) string.
  2590.  
  2591.    Upon return, any arg will be pointed to with argstart and will be
  2592.    arglen long.  Note that we don't parse that arg since it will just
  2593.    be printed out again.
  2594. */
  2595.  
  2596. static char *
  2597. get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
  2598.      register U_CHAR *ibp;
  2599.      register U_CHAR *limit;
  2600.      U_CHAR **argstart;        /* point to command arg */
  2601.      int *arglen, *cmdlen;    /* how long they are */
  2602. {
  2603.   long linsize;
  2604.   register U_CHAR *numptr;    /* temp for arg parsing */
  2605.  
  2606.   *arglen = 0;
  2607.  
  2608.   SKIP_WHITE_SPACE (ibp);
  2609.  
  2610.   if (ibp >= limit) return NULL;
  2611.  
  2612.   linsize = limit - ibp;
  2613.   
  2614.   /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
  2615.   if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
  2616.     *cmdlen = 10;
  2617.     return "NOTREACHED";
  2618.   }
  2619.   if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
  2620.     *cmdlen = 8;
  2621.     return "ARGSUSED";
  2622.   }
  2623.   if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
  2624.     *cmdlen = 11;
  2625.     return "LINTLIBRARY";
  2626.   }
  2627.   if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
  2628.     *cmdlen = 7;
  2629.     ibp += 7; linsize -= 7;
  2630.     if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
  2631.  
  2632.     /* OK, read a number */
  2633.     for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
  2634.      numptr++);
  2635.     *arglen = numptr - *argstart;
  2636.     return "VARARGS";
  2637.   }
  2638.   return NULL;
  2639. }
  2640.  
  2641. /*
  2642.  * The main loop of the program.
  2643.  *
  2644.  * Read characters from the input stack, transferring them to the
  2645.  * output buffer OP.
  2646.  *
  2647.  * Macros are expanded and push levels on the input stack.
  2648.  * At the end of such a level it is popped off and we keep reading.
  2649.  * At the end of any other kind of level, we return.
  2650.  * #-directives are handled, except within macros.
  2651.  *
  2652.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  2653.  * and insert them when appropriate.  This is set while scanning macro
  2654.  * arguments before substitution.  It is zero when scanning for final output.
  2655.  *   There are three types of Newline markers:
  2656.  *   * Newline -  follows a macro name that was not expanded
  2657.  *     because it appeared inside an expansion of the same macro.
  2658.  *     This marker prevents future expansion of that identifier.
  2659.  *     When the input is rescanned into the final output, these are deleted.
  2660.  *     These are also deleted by ## concatenation.
  2661.  *   * Newline Space (or Newline and any other whitespace character)
  2662.  *     stands for a place that tokens must be separated or whitespace
  2663.  *     is otherwise desirable, but where the ANSI standard specifies there
  2664.  *     is no whitespace.  This marker turns into a Space (or whichever other
  2665.  *     whitespace char appears in the marker) in the final output,
  2666.  *     but it turns into nothing in an argument that is stringified with #.
  2667.  *     Such stringified arguments are the only place where the ANSI standard
  2668.  *     specifies with precision that whitespace may not appear.
  2669.  *
  2670.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  2671.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  2672.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  2673.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  2674.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  2675.  */
  2676.  
  2677. static void
  2678. rescan (op, output_marks)
  2679.      FILE_BUF *op;
  2680.      int output_marks;
  2681. {
  2682.   /* Character being scanned in main loop.  */
  2683.   register U_CHAR c;
  2684.  
  2685.   /* Length of pending accumulated identifier.  */
  2686.   register int ident_length = 0;
  2687.  
  2688.   /* Hash code of pending accumulated identifier.  */
  2689.   register int hash = 0;
  2690.  
  2691.   /* Current input level (&instack[indepth]).  */
  2692.   FILE_BUF *ip;
  2693.  
  2694.   /* Pointer for scanning input.  */
  2695.   register U_CHAR *ibp;
  2696.  
  2697.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  2698.   register U_CHAR *limit;
  2699.  
  2700.   /* Pointer for storing output.  */
  2701.   register U_CHAR *obp;
  2702.  
  2703.   /* REDO_CHAR is nonzero if we are processing an identifier
  2704.      after backing up over the terminating character.
  2705.      Sometimes we process an identifier without backing up over
  2706.      the terminating character, if the terminating character
  2707.      is not special.  Backing up is done so that the terminating character
  2708.      will be dispatched on again once the identifier is dealt with.  */
  2709.   int redo_char = 0;
  2710.  
  2711.   /* 1 if within an identifier inside of which a concatenation
  2712.      marker (Newline -) has been seen.  */
  2713.   int concatenated = 0;
  2714.  
  2715.   /* While scanning a comment or a string constant,
  2716.      this records the line it started on, for error messages.  */
  2717.   int start_line;
  2718.  
  2719.   /* Record position of last `real' newline.  */
  2720.   U_CHAR *beg_of_line;
  2721.  
  2722. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  2723.  
  2724. #define POPMACRO \
  2725. do { ip->macro->type = T_MACRO;        \
  2726.      if (ip->free_ptr) free (ip->free_ptr);    \
  2727.      --indepth; } while (0)
  2728.  
  2729. /* Reload `rescan's local variables that describe the current
  2730.    level of the input stack.  */
  2731.  
  2732. #define RECACHE  \
  2733. do { ip = &instack[indepth];        \
  2734.      ibp = ip->bufp;            \
  2735.      limit = ip->buf + ip->length;    \
  2736.      op->bufp = obp;            \
  2737.      check_expand (op, limit - ibp);    \
  2738.      beg_of_line = 0;            \
  2739.      obp = op->bufp; } while (0)
  2740.  
  2741.   if (no_output && instack[indepth].fname != 0)
  2742.     skip_if_group (&instack[indepth], 1, NULL);
  2743.  
  2744.   obp = op->bufp;
  2745.   RECACHE;
  2746.  
  2747.   beg_of_line = ibp;
  2748.  
  2749.   /* Our caller must always put a null after the end of
  2750.      the input at each input stack level.  */
  2751.   if (*limit != 0)
  2752.     abort ();
  2753.  
  2754.   while (1) {
  2755.     c = *ibp++;
  2756.     *obp++ = c;
  2757.  
  2758.     switch (c) {
  2759.     case '\\':
  2760.       if (*ibp == '\n' && !ip->macro) {
  2761.     /* At the top level, always merge lines ending with backslash-newline,
  2762.        even in middle of identifier.  But do not merge lines in a macro,
  2763.        since backslash might be followed by a newline-space marker.  */
  2764.     ++ibp;
  2765.     ++ip->lineno;
  2766.     --obp;        /* remove backslash from obuf */
  2767.     break;
  2768.       }
  2769.       /* If ANSI, backslash is just another character outside a string.  */
  2770.       if (!traditional)
  2771.     goto randomchar;
  2772.       /* Otherwise, backslash suppresses specialness of following char,
  2773.      so copy it here to prevent the switch from seeing it.
  2774.      But first get any pending identifier processed.  */
  2775.       if (ident_length > 0)
  2776.     goto specialchar;
  2777.       if (ibp < limit)
  2778.     *obp++ = *ibp++;
  2779.       break;
  2780.  
  2781.     case '%':
  2782.       if (ident_length || ip->macro || traditional)
  2783.     goto randomchar;
  2784.       while (*ibp == '\\' && ibp[1] == '\n') {
  2785.     ibp += 2;
  2786.     ++ip->lineno;
  2787.       }
  2788.       if (*ibp != ':')
  2789.     break;
  2790.       /* Treat this %: digraph as if it were #.  */
  2791.       /* Fall through.  */
  2792.  
  2793.     case '#':
  2794.       if (assertions_flag) {
  2795.     if (ident_length)
  2796.       goto specialchar;
  2797.     /* Copy #foo (bar lose) without macro expansion.  */
  2798.     obp[-1] = '#';    /* In case it was '%'. */
  2799.     SKIP_WHITE_SPACE (ibp);
  2800.     while (is_idchar[*ibp])
  2801.       *obp++ = *ibp++;
  2802.     SKIP_WHITE_SPACE (ibp);
  2803.     if (*ibp == '(') {
  2804.       ip->bufp = ibp;
  2805.       skip_paren_group (ip);
  2806.       bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
  2807.       obp += ip->bufp - ibp;
  2808.       ibp = ip->bufp;
  2809.     }
  2810.     break;
  2811.       }
  2812.  
  2813.       /* If this is expanding a macro definition, don't recognize
  2814.      preprocessing directives.  */
  2815.       if (ip->macro != 0)
  2816.     goto randomchar;
  2817.       /* If this is expand_into_temp_buffer,
  2818.      don't recognize them either.  Warn about them
  2819.      only after an actual newline at this level,
  2820.      not at the beginning of the input level.  */
  2821.       if (! ip->fname) {
  2822.     if (ip->buf != beg_of_line)
  2823.       warning ("preprocessing directive not recognized within macro arg");
  2824.     goto randomchar;
  2825.       }
  2826.       if (ident_length)
  2827.     goto specialchar;
  2828.  
  2829.       
  2830.       /* # keyword: a # must be first nonblank char on the line */
  2831.       if (beg_of_line == 0)
  2832.     goto randomchar;
  2833.       {
  2834.     U_CHAR *bp;
  2835.  
  2836.     /* Scan from start of line, skipping whitespace, comments
  2837.        and backslash-newlines, and see if we reach this #.
  2838.        If not, this # is not special.  */
  2839.     bp = beg_of_line;
  2840.     /* If -traditional, require # to be at beginning of line.  */
  2841.     if (!traditional) {
  2842.       while (1) {
  2843.         if (is_hor_space[*bp])
  2844.           bp++;
  2845.         else if (*bp == '\\' && bp[1] == '\n')
  2846.           bp += 2;
  2847.         else if (*bp == '/' && bp[1] == '*') {
  2848.           bp += 2;
  2849.           while (!(*bp == '*' && bp[1] == '/'))
  2850.         bp++;
  2851.           bp += 2;
  2852.         }
  2853.         /* There is no point in trying to deal with C++ // comments here,
  2854.            because if there is one, then this # must be part of the
  2855.            comment and we would never reach here.  */
  2856.         else break;
  2857.       }
  2858.       if (c == '%') {
  2859.         if (bp[0] != '%')
  2860.           break;
  2861.         while (bp[1] == '\\' && bp[2] == '\n')
  2862.           bp += 2;
  2863.         if (bp + 1 != ibp)
  2864.           break;
  2865.         /* %: appears at start of line; skip past the ':' too.  */
  2866.         bp++;
  2867.         ibp++;
  2868.       }
  2869.     }
  2870.     if (bp + 1 != ibp)
  2871.       goto randomchar;
  2872.       }
  2873.  
  2874.       /* This # can start a directive.  */
  2875.  
  2876.       --obp;        /* Don't copy the '#' */
  2877.  
  2878.       ip->bufp = ibp;
  2879.       op->bufp = obp;
  2880.       if (! handle_directive (ip, op)) {
  2881. #ifdef USE_C_ALLOCA
  2882.     alloca (0);
  2883. #endif
  2884.     /* Not a known directive: treat it as ordinary text.
  2885.        IP, OP, IBP, etc. have not been changed.  */
  2886.     if (no_output && instack[indepth].fname) {
  2887.       /* If not generating expanded output,
  2888.          what we do with ordinary text is skip it.
  2889.          Discard everything until next # directive.  */
  2890.       skip_if_group (&instack[indepth], 1, 0);
  2891.       RECACHE;
  2892.       beg_of_line = ibp;
  2893.       break;
  2894.     }
  2895.     *obp++ = '#';    /* Copy # (even if it was originally %:).  */
  2896.     /* Don't expand an identifier that could be a macro directive.
  2897.        (Section 3.8.3 of the ANSI C standard)            */
  2898.     SKIP_WHITE_SPACE (ibp);
  2899.     if (is_idstart[*ibp])
  2900.       {
  2901.         *obp++ = *ibp++;
  2902.         while (is_idchar[*ibp])
  2903.           *obp++ = *ibp++;
  2904.       }
  2905.     goto randomchar;
  2906.       }
  2907. #ifdef USE_C_ALLOCA
  2908.       alloca (0);
  2909. #endif
  2910.       /* A # directive has been successfully processed.  */
  2911.       /* If not generating expanded output, ignore everything until
  2912.      next # directive.  */
  2913.       if (no_output && instack[indepth].fname)
  2914.     skip_if_group (&instack[indepth], 1, 0);
  2915.       obp = op->bufp;
  2916.       RECACHE;
  2917.       beg_of_line = ibp;
  2918.       break;
  2919.  
  2920.     case '\"':            /* skip quoted string */
  2921.     case '\'':
  2922.       /* A single quoted string is treated like a double -- some
  2923.      programs (e.g., troff) are perverse this way */
  2924.  
  2925.       if (ident_length)
  2926.     goto specialchar;
  2927.  
  2928.       start_line = ip->lineno;
  2929.  
  2930.       /* Skip ahead to a matching quote.  */
  2931.  
  2932.       while (1) {
  2933.     if (ibp >= limit) {
  2934.       if (ip->macro != 0) {
  2935.         /* try harder: this string crosses a macro expansion boundary.
  2936.            This can happen naturally if -traditional.
  2937.            Otherwise, only -D can make a macro with an unmatched quote.  */
  2938.         POPMACRO;
  2939.         RECACHE;
  2940.         continue;
  2941.       }
  2942.       if (!traditional) {
  2943.         error_with_line (line_for_error (start_line),
  2944.                  "unterminated string or character constant");
  2945.         error_with_line (multiline_string_line,
  2946.                  "possible real start of unterminated constant");
  2947.         multiline_string_line = 0;
  2948.       }
  2949.       break;
  2950.     }
  2951.     *obp++ = *ibp;
  2952.     switch (*ibp++) {
  2953.     case '\n':
  2954.       ++ip->lineno;
  2955.       ++op->lineno;
  2956.       /* Traditionally, end of line ends a string constant with no error.
  2957.          So exit the loop and record the new line.  */
  2958.       if (traditional) {
  2959.         beg_of_line = ibp;
  2960.         goto while2end;
  2961.       }
  2962.       if (c == '\'') {
  2963.         error_with_line (line_for_error (start_line),
  2964.                  "unterminated character constant");
  2965.         goto while2end;
  2966.       }
  2967.       if (pedantic && multiline_string_line == 0) {
  2968.         pedwarn_with_line (line_for_error (start_line),
  2969.                    "string constant runs past end of line");
  2970.       }
  2971.       if (multiline_string_line == 0)
  2972.         multiline_string_line = ip->lineno - 1;
  2973.       break;
  2974.  
  2975.     case '\\':
  2976.       if (ibp >= limit)
  2977.         break;
  2978.       if (*ibp == '\n') {
  2979.         /* Backslash newline is replaced by nothing at all,
  2980.            but keep the line counts correct.  */
  2981.         --obp;
  2982.         ++ibp;
  2983.         ++ip->lineno;
  2984.       } else {
  2985.         /* ANSI stupidly requires that in \\ the second \
  2986.            is *not* prevented from combining with a newline.  */
  2987.         while (*ibp == '\\' && ibp[1] == '\n') {
  2988.           ibp += 2;
  2989.           ++ip->lineno;
  2990.         }
  2991.         *obp++ = *ibp++;
  2992.       }
  2993.       break;
  2994.  
  2995.     case '\"':
  2996.     case '\'':
  2997.       if (ibp[-1] == c)
  2998.         goto while2end;
  2999.       break;
  3000.     }
  3001.       }
  3002.     while2end:
  3003.       break;
  3004.  
  3005.     case '/':
  3006.       if (*ibp == '\\' && ibp[1] == '\n')
  3007.     newline_fix (ibp);
  3008.  
  3009.       if (*ibp != '*'
  3010.       && !(cplusplus_comments && *ibp == '/'))
  3011.     goto randomchar;
  3012.       if (ip->macro != 0)
  3013.     goto randomchar;
  3014.       if (ident_length)
  3015.     goto specialchar;
  3016.  
  3017.       if (*ibp == '/') {
  3018.     /* C++ style comment... */
  3019.     start_line = ip->lineno;
  3020.  
  3021.     /* Comments are equivalent to spaces. */
  3022.     if (! put_out_comments)
  3023.       obp[-1] = ' ';
  3024.  
  3025.     {
  3026.       U_CHAR *before_bp = ibp;
  3027.  
  3028.       while (++ibp < limit) {
  3029.         if (*ibp == '\n') {
  3030.           if (ibp[-1] != '\\') {
  3031.         if (put_out_comments) {
  3032.           bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  3033.           obp += ibp - before_bp;
  3034.         }
  3035.         break;
  3036.           }
  3037.           ++ip->lineno;
  3038.           /* Copy the newline into the output buffer, in order to
  3039.          avoid the pain of a #line every time a multiline comment
  3040.          is seen.  */
  3041.           if (!put_out_comments)
  3042.         *obp++ = '\n';
  3043.           ++op->lineno;
  3044.         }
  3045.       }
  3046.       break;
  3047.     }
  3048.       }
  3049.  
  3050.       /* Ordinary C comment.  Skip it, optionally copying it to output.  */
  3051.  
  3052.       start_line = ip->lineno;
  3053.  
  3054.       ++ibp;            /* Skip the star. */
  3055.  
  3056.       /* If this cpp is for lint, we peek inside the comments: */
  3057.       if (for_lint) {
  3058.     U_CHAR *argbp;
  3059.     int cmdlen, arglen;
  3060.     char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
  3061.  
  3062.     if (lintcmd != NULL) {
  3063.       op->bufp = obp;
  3064.       check_expand (op, cmdlen + arglen + 14);
  3065.       obp = op->bufp;
  3066.       /* I believe it is always safe to emit this newline: */
  3067.       obp[-1] = '\n';
  3068.       bcopy ("#pragma lint ", (char *) obp, 13);
  3069.       obp += 13;
  3070.       bcopy (lintcmd, (char *) obp, cmdlen);
  3071.       obp += cmdlen;
  3072.  
  3073.       if (arglen != 0) {
  3074.         *(obp++) = ' ';
  3075.         bcopy (argbp, (char *) obp, arglen);
  3076.         obp += arglen;
  3077.       }
  3078.  
  3079.       /* OK, now bring us back to the state we were in before we entered
  3080.          this branch.  We need #line because the #pragma's newline always
  3081.          messes up the line count.  */
  3082.       op->bufp = obp;
  3083.       output_line_directive (ip, op, 0, same_file);
  3084.       check_expand (op, limit - ibp + 2);
  3085.       obp = op->bufp;
  3086.       *(obp++) = '/';
  3087.     }
  3088.       }
  3089.  
  3090.       /* Comments are equivalent to spaces.
  3091.      Note that we already output the slash; we might not want it.
  3092.      For -traditional, a comment is equivalent to nothing.  */
  3093.       if (! put_out_comments) {
  3094.     if (traditional)
  3095.       obp--;
  3096.     else
  3097.       obp[-1] = ' ';
  3098.       }
  3099.       else
  3100.     *obp++ = '*';
  3101.  
  3102.       {
  3103.     U_CHAR *before_bp = ibp;
  3104.  
  3105.     while (ibp < limit) {
  3106.       switch (*ibp++) {
  3107.       case '/':
  3108.         if (warn_comments && *ibp == '*')
  3109.           warning ("`/*' within comment");
  3110.         break;
  3111.       case '*':
  3112.         if (*ibp == '\\' && ibp[1] == '\n')
  3113.           newline_fix (ibp);
  3114.         if (ibp >= limit || *ibp == '/')
  3115.           goto comment_end;
  3116.         break;
  3117.       case '\n':
  3118.         ++ip->lineno;
  3119.         /* Copy the newline into the output buffer, in order to
  3120.            avoid the pain of a #line every time a multiline comment
  3121.            is seen.  */
  3122.         if (!put_out_comments)
  3123.           *obp++ = '\n';
  3124.         ++op->lineno;
  3125.       }
  3126.     }
  3127.       comment_end:
  3128.  
  3129.     if (ibp >= limit)
  3130.       error_with_line (line_for_error (start_line),
  3131.                "unterminated comment");
  3132.     else {
  3133.       ibp++;
  3134.       if (put_out_comments) {
  3135.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  3136.         obp += ibp - before_bp;
  3137.       }
  3138.     }
  3139.       }
  3140.       break;
  3141.  
  3142.     case '$':
  3143.       if (!dollars_in_ident)
  3144.     goto randomchar;
  3145.       goto letter;
  3146.  
  3147.     case '0': case '1': case '2': case '3': case '4':
  3148.     case '5': case '6': case '7': case '8': case '9':
  3149.       /* If digit is not part of identifier, it starts a number,
  3150.      which means that following letters are not an identifier.
  3151.      "0x5" does not refer to an identifier "x5".
  3152.      So copy all alphanumerics that follow without accumulating
  3153.      as an identifier.  Periods also, for sake of "3.e7".  */
  3154.  
  3155.       if (ident_length == 0) {
  3156.     for (;;) {
  3157.       while (ibp[0] == '\\' && ibp[1] == '\n') {
  3158.         ++ip->lineno;
  3159.         ibp += 2;
  3160.       }
  3161.       c = *ibp++;
  3162.       if (!is_idchar[c] && c != '.') {
  3163.         --ibp;
  3164.         break;
  3165.       }
  3166.       *obp++ = c;
  3167.       /* A sign can be part of a preprocessing number
  3168.          if it follows an e.  */
  3169.       if (c == 'e' || c == 'E') {
  3170.         while (ibp[0] == '\\' && ibp[1] == '\n') {
  3171.           ++ip->lineno;
  3172.           ibp += 2;
  3173.         }
  3174.         if (*ibp == '+' || *ibp == '-') {
  3175.           *obp++ = *ibp++;
  3176.           /* But traditional C does not let the token go past the sign.  */
  3177.           if (traditional)
  3178.         break;
  3179.         }
  3180.       }
  3181.     }
  3182.     break;
  3183.       }
  3184.       /* fall through */
  3185.  
  3186.     case '_':
  3187.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  3188.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  3189.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  3190.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  3191.     case 'y': case 'z':
  3192.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  3193.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  3194.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  3195.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  3196.     case 'Y': case 'Z':
  3197.     letter:
  3198.       ident_length++;
  3199.       /* Compute step of hash function, to avoid a proc call on every token */
  3200.       hash = HASHSTEP (hash, c);
  3201.       break;
  3202.  
  3203.     case '\n':
  3204.       if (ip->fname == 0 && *ibp == '-') {
  3205.     /* Newline - inhibits expansion of preceding token.
  3206.        If expanding a macro arg, we keep the newline -.
  3207.        In final output, it is deleted.
  3208.        We recognize Newline - in macro bodies and macro args.  */
  3209.     if (! concatenated) {
  3210.       ident_length = 0;
  3211.       hash = 0;
  3212.     }
  3213.     ibp++;
  3214.     if (!output_marks) {
  3215.       obp--;
  3216.     } else {
  3217.       /* If expanding a macro arg, keep the newline -.  */
  3218.       *obp++ = '-';
  3219.     }
  3220.     break;
  3221.       }
  3222.  
  3223.       /* If reprocessing a macro expansion, newline is a special marker.  */
  3224.       else if (ip->macro != 0) {
  3225.     /* Newline White is a "funny space" to separate tokens that are
  3226.        supposed to be separate but without space between.
  3227.        Here White means any whitespace character.
  3228.        Newline - marks a recursive macro use that is not
  3229.        supposed to be expandable.  */
  3230.  
  3231.     if (is_space[*ibp]) {
  3232.       /* Newline Space does not prevent expansion of preceding token
  3233.          so expand the preceding token and then come back.  */
  3234.       if (ident_length > 0)
  3235.         goto specialchar;
  3236.  
  3237.       /* If generating final output, newline space makes a space.  */
  3238.       if (!output_marks) {
  3239.         obp[-1] = *ibp++;
  3240.         /* And Newline Newline makes a newline, so count it.  */
  3241.         if (obp[-1] == '\n')
  3242.           op->lineno++;
  3243.       } else {
  3244.         /* If expanding a macro arg, keep the newline space.
  3245.            If the arg gets stringified, newline space makes nothing.  */
  3246.         *obp++ = *ibp++;
  3247.       }
  3248.     } else abort ();    /* Newline followed by something random?  */
  3249.     break;
  3250.       }
  3251.  
  3252.       /* If there is a pending identifier, handle it and come back here.  */
  3253.       if (ident_length > 0)
  3254.     goto specialchar;
  3255.  
  3256.       beg_of_line = ibp;
  3257.  
  3258.       /* Update the line counts and output a #line if necessary.  */
  3259.       ++ip->lineno;
  3260.       ++op->lineno;
  3261.       if (ip->lineno != op->lineno) {
  3262.     op->bufp = obp;
  3263.     output_line_directive (ip, op, 1, same_file);
  3264.     check_expand (op, limit - ibp);
  3265.     obp = op->bufp;
  3266.       }
  3267.       break;
  3268.  
  3269.       /* Come here either after (1) a null character that is part of the input
  3270.      or (2) at the end of the input, because there is a null there.  */
  3271.     case 0:
  3272.       if (ibp <= limit)
  3273.     /* Our input really contains a null character.  */
  3274.     goto randomchar;
  3275.  
  3276.       /* At end of a macro-expansion level, pop it and read next level.  */
  3277.       if (ip->macro != 0) {
  3278.     obp--;
  3279.     ibp--;
  3280.     /* If traditional, and we have an identifier that ends here,
  3281.        process it now, so we get the right error for recursion.  */
  3282.     if (traditional && ident_length
  3283.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  3284.       redo_char = 1;
  3285.       goto randomchar;
  3286.     }
  3287.     POPMACRO;
  3288.     RECACHE;
  3289.     break;
  3290.       }
  3291.  
  3292.       /* If we don't have a pending identifier,
  3293.      return at end of input.  */
  3294.       if (ident_length == 0) {
  3295.     obp--;
  3296.     ibp--;
  3297.     op->bufp = obp;
  3298.     ip->bufp = ibp;
  3299.     goto ending;
  3300.       }
  3301.  
  3302.       /* If we do have a pending identifier, just consider this null
  3303.      a special character and arrange to dispatch on it again.
  3304.      The second time, IDENT_LENGTH will be zero so we will return.  */
  3305.  
  3306.       /* Fall through */
  3307.  
  3308. specialchar:
  3309.  
  3310.       /* Handle the case of a character such as /, ', " or null
  3311.      seen following an identifier.  Back over it so that
  3312.      after the identifier is processed the special char
  3313.      will be dispatched on again.  */
  3314.  
  3315.       ibp--;
  3316.       obp--;
  3317.       redo_char = 1;
  3318.  
  3319.     default:
  3320.  
  3321. randomchar:
  3322.  
  3323.       if (ident_length > 0) {
  3324.     register HASHNODE *hp;
  3325.  
  3326.     /* We have just seen an identifier end.  If it's a macro, expand it.
  3327.  
  3328.        IDENT_LENGTH is the length of the identifier
  3329.        and HASH is its hash code.
  3330.  
  3331.        The identifier has already been copied to the output,
  3332.        so if it is a macro we must remove it.
  3333.  
  3334.        If REDO_CHAR is 0, the char that terminated the identifier
  3335.        has been skipped in the output and the input.
  3336.        OBP-IDENT_LENGTH-1 points to the identifier.
  3337.        If the identifier is a macro, we must back over the terminator.
  3338.  
  3339.        If REDO_CHAR is 1, the terminating char has already been
  3340.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  3341.  
  3342.     if (!pcp_outfile || pcp_inside_if) {
  3343.       for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  3344.            hp = hp->next) {
  3345.         
  3346.         if (hp->length == ident_length) {
  3347.           int obufp_before_macroname;
  3348.           int op_lineno_before_macroname;
  3349.           register int i = ident_length;
  3350.           register U_CHAR *p = hp->name;
  3351.           register U_CHAR *q = obp - i;
  3352.           int disabled;
  3353.           
  3354.           if (! redo_char)
  3355.         q--;
  3356.           
  3357.           do {        /* All this to avoid a strncmp () */
  3358.         if (*p++ != *q++)
  3359.           goto hashcollision;
  3360.           } while (--i);
  3361.           
  3362.           /* We found a use of a macro name.
  3363.          see if the context shows it is a macro call.  */
  3364.           
  3365.           /* Back up over terminating character if not already done.  */
  3366.           if (! redo_char) {
  3367.         ibp--;
  3368.         obp--;
  3369.           }
  3370.           
  3371.           /* Save this as a displacement from the beginning of the output
  3372.          buffer.  We can not save this as a position in the output
  3373.          buffer, because it may get realloc'ed by RECACHE.  */
  3374.           obufp_before_macroname = (obp - op->buf) - ident_length;
  3375.           op_lineno_before_macroname = op->lineno;
  3376.           
  3377.           if (hp->type == T_PCSTRING) {
  3378.         pcstring_used (hp); /* Mark the definition of this key
  3379.                        as needed, ensuring that it
  3380.                        will be output.  */
  3381.         break;        /* Exit loop, since the key cannot have a
  3382.                    definition any longer.  */
  3383.           }
  3384.  
  3385.           /* Record whether the macro is disabled.  */
  3386.           disabled = hp->type == T_DISABLED;
  3387.           
  3388.           /* This looks like a macro ref, but if the macro was disabled,
  3389.          just copy its name and put in a marker if requested.  */
  3390.           
  3391.           if (disabled) {
  3392. #if 0
  3393.         /* This error check caught useful cases such as
  3394.            #define foo(x,y) bar (x (y,0), y)
  3395.            foo (foo, baz)  */
  3396.         if (traditional)
  3397.           error ("recursive use of macro `%s'", hp->name);
  3398. #endif
  3399.         
  3400.         if (output_marks) {
  3401.           check_expand (op, limit - ibp + 2);
  3402.           *obp++ = '\n';
  3403.           *obp++ = '-';
  3404.         }
  3405.         break;
  3406.           }
  3407.           
  3408.           /* If macro wants an arglist, verify that a '(' follows.
  3409.          first skip all whitespace, copying it to the output
  3410.          after the macro name.  Then, if there is no '(',
  3411.          decide this is not a macro call and leave things that way.  */
  3412.           if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  3413.           && hp->value.defn->nargs >= 0)
  3414.         {
  3415.           U_CHAR *old_ibp = ibp;
  3416.           U_CHAR *old_obp = obp;
  3417.           int old_iln = ip->lineno;
  3418.           int old_oln = op->lineno;
  3419.           
  3420.           while (1) {
  3421.             /* Scan forward over whitespace, copying it to the output.  */
  3422.             if (ibp == limit && ip->macro != 0) {
  3423.               POPMACRO;
  3424.               RECACHE;
  3425.               old_ibp = ibp;
  3426.               old_obp = obp;
  3427.               old_iln = ip->lineno;
  3428.               old_oln = op->lineno;
  3429.             }
  3430.             /* A comment: copy it unchanged or discard it.  */
  3431.             else if (*ibp == '/' && ibp[1] == '*') {
  3432.               if (put_out_comments) {
  3433.             *obp++ = '/';
  3434.             *obp++ = '*';
  3435.               } else if (! traditional) {
  3436.             *obp++ = ' ';
  3437.               }
  3438.               ibp += 2;
  3439.               while (ibp + 1 != limit
  3440.                  && !(ibp[0] == '*' && ibp[1] == '/')) {
  3441.             /* We need not worry about newline-marks,
  3442.                since they are never found in comments.  */
  3443.             if (*ibp == '\n') {
  3444.               /* Newline in a file.  Count it.  */
  3445.               ++ip->lineno;
  3446.               ++op->lineno;
  3447.             }
  3448.             if (put_out_comments)
  3449.               *obp++ = *ibp++;
  3450.             else
  3451.               ibp++;
  3452.               }
  3453.               ibp += 2;
  3454.               if (put_out_comments) {
  3455.             *obp++ = '*';
  3456.             *obp++ = '/';
  3457.               }
  3458.             }
  3459.             else if (is_space[*ibp]) {
  3460.               *obp++ = *ibp++;
  3461.               if (ibp[-1] == '\n') {
  3462.             if (ip->macro == 0) {
  3463.               /* Newline in a file.  Count it.  */
  3464.               ++ip->lineno;
  3465.               ++op->lineno;
  3466.             } else if (!output_marks) {
  3467.               /* A newline mark, and we don't want marks
  3468.                  in the output.  If it is newline-hyphen,
  3469.                  discard it entirely.  Otherwise, it is
  3470.                  newline-whitechar, so keep the whitechar.  */
  3471.               obp--;
  3472.               if (*ibp == '-')
  3473.                 ibp++;
  3474.               else {
  3475.                 if (*ibp == '\n')
  3476.                   ++op->lineno;
  3477.                 *obp++ = *ibp++;
  3478.               }
  3479.             } else {
  3480.               /* A newline mark; copy both chars to the output.  */
  3481.               *obp++ = *ibp++;
  3482.             }
  3483.               }
  3484.             }
  3485.             else break;
  3486.           }
  3487.           if (*ibp != '(') {
  3488.             /* It isn't a macro call.
  3489.                Put back the space that we just skipped.  */
  3490.             ibp = old_ibp;
  3491.             obp = old_obp;
  3492.             ip->lineno = old_iln;
  3493.             op->lineno = old_oln;
  3494.             /* Exit the for loop.  */
  3495.             break;
  3496.           }
  3497.         }
  3498.           
  3499.           /* This is now known to be a macro call.
  3500.          Discard the macro name from the output,
  3501.          along with any following whitespace just copied,
  3502.          but preserve newlines if not outputting marks since this
  3503.          is more likely to do the right thing with line numbers.  */
  3504.           obp = op->buf + obufp_before_macroname;
  3505.           if (output_marks)
  3506.         op->lineno = op_lineno_before_macroname;
  3507.           else {
  3508.         int newlines = op->lineno - op_lineno_before_macroname;
  3509.         while (0 < newlines--)
  3510.           *obp++ = '\n';
  3511.           }
  3512.  
  3513.           /* Prevent accidental token-pasting with a character
  3514.          before the macro call.  */
  3515.           if (!traditional && obp != op->buf) {
  3516.         switch (obp[-1]) {
  3517.         case '!':  case '%':  case '&':  case '*':
  3518.         case '+':  case '-':  case '/':  case ':':
  3519.         case '<':  case '=':  case '>':  case '^':
  3520.         case '|':
  3521.           /* If we are expanding a macro arg, make a newline marker
  3522.              to separate the tokens.  If we are making real output,
  3523.              a plain space will do.  */
  3524.           if (output_marks)
  3525.             *obp++ = '\n';
  3526.           *obp++ = ' ';
  3527.         }
  3528.           }
  3529.  
  3530.           /* Expand the macro, reading arguments as needed,
  3531.          and push the expansion on the input stack.  */
  3532.           ip->bufp = ibp;
  3533.           op->bufp = obp;
  3534.           macroexpand (hp, op);
  3535.           
  3536.           /* Reexamine input stack, since macroexpand has pushed
  3537.          a new level on it.  */
  3538.           obp = op->bufp;
  3539.           RECACHE;
  3540.           break;
  3541.         }
  3542. hashcollision:
  3543.         ;
  3544.       }            /* End hash-table-search loop */
  3545.     }
  3546.     ident_length = hash = 0; /* Stop collecting identifier */
  3547.     redo_char = 0;
  3548.     concatenated = 0;
  3549.       }                /* End if (ident_length > 0) */
  3550.     }                /* End switch */
  3551.   }                /* End per-char loop */
  3552.  
  3553.   /* Come here to return -- but first give an error message
  3554.      if there was an unterminated successful conditional.  */
  3555.  ending:
  3556.   if (if_stack != ip->if_stack)
  3557.     {
  3558.       char *str;
  3559.  
  3560.       switch (if_stack->type)
  3561.     {
  3562.     case T_IF:
  3563.       str = "if";
  3564.       break;
  3565.     case T_IFDEF:
  3566.       str = "ifdef";
  3567.       break;
  3568.     case T_IFNDEF:
  3569.       str = "ifndef";
  3570.       break;
  3571.     case T_ELSE:
  3572.       str = "else";
  3573.       break;
  3574.     case T_ELIF:
  3575.       str = "elif";
  3576.       break;
  3577.     default:
  3578.       abort ();
  3579.     }
  3580.  
  3581.       error_with_line (line_for_error (if_stack->lineno),
  3582.                "unterminated `#%s' conditional", str);
  3583.   }
  3584.   if_stack = ip->if_stack;
  3585. }
  3586.  
  3587. /*
  3588.  * Rescan a string into a temporary buffer and return the result
  3589.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  3590.  *
  3591.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  3592.  * and insert such markers when appropriate.  See `rescan' for details.
  3593.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  3594.  * before substitution; it is 0 for other uses.
  3595.  */
  3596. static FILE_BUF
  3597. expand_to_temp_buffer (buf, limit, output_marks, assertions)
  3598.      U_CHAR *buf, *limit;
  3599.      int output_marks, assertions;
  3600. {
  3601.   register FILE_BUF *ip;
  3602.   FILE_BUF obuf;
  3603.   int length = limit - buf;
  3604.   U_CHAR *buf1;
  3605.   int odepth = indepth;
  3606.   int save_assertions_flag = assertions_flag;
  3607.  
  3608.   assertions_flag = assertions;
  3609.  
  3610.   if (length < 0)
  3611.     abort ();
  3612.  
  3613.   /* Set up the input on the input stack.  */
  3614.  
  3615.   buf1 = (U_CHAR *) alloca (length + 1);
  3616.   {
  3617.     register U_CHAR *p1 = buf;
  3618.     register U_CHAR *p2 = buf1;
  3619.  
  3620.     while (p1 != limit)
  3621.       *p2++ = *p1++;
  3622.   }
  3623.   buf1[length] = 0;
  3624.  
  3625.   /* Set up to receive the output.  */
  3626.  
  3627.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  3628.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  3629.   obuf.fname = 0;
  3630.   obuf.macro = 0;
  3631.   obuf.free_ptr = 0;
  3632.  
  3633.   CHECK_DEPTH ({return obuf;});
  3634.  
  3635.   ++indepth;
  3636.  
  3637.   ip = &instack[indepth];
  3638.   ip->fname = 0;
  3639.   ip->nominal_fname = 0;
  3640.   ip->system_header_p = 0;
  3641.   ip->macro = 0;
  3642.   ip->free_ptr = 0;
  3643.   ip->length = length;
  3644.   ip->buf = ip->bufp = buf1;
  3645.   ip->if_stack = if_stack;
  3646.  
  3647.   ip->lineno = obuf.lineno = 1;
  3648.  
  3649.   /* Scan the input, create the output.  */
  3650.   rescan (&obuf, output_marks);
  3651.  
  3652.   /* Pop input stack to original state.  */
  3653.   --indepth;
  3654.  
  3655.   if (indepth != odepth)
  3656.     abort ();
  3657.  
  3658.   /* Record the output.  */
  3659.   obuf.length = obuf.bufp - obuf.buf;
  3660.  
  3661.   assertions_flag = save_assertions_flag;
  3662.   return obuf;
  3663. }
  3664.  
  3665. /*
  3666.  * Process a # directive.  Expects IP->bufp to point after the '#', as in
  3667.  * `#define foo bar'.  Passes to the directive handler
  3668.  * (do_define, do_include, etc.): the addresses of the 1st and
  3669.  * last chars of the directive (starting immediately after the #
  3670.  * keyword), plus op and the keyword table pointer.  If the directive
  3671.  * contains comments it is copied into a temporary buffer sans comments
  3672.  * and the temporary buffer is passed to the directive handler instead.
  3673.  * Likewise for backslash-newlines.
  3674.  *
  3675.  * Returns nonzero if this was a known # directive.
  3676.  * Otherwise, returns zero, without advancing the input pointer.
  3677.  */
  3678.  
  3679. static int
  3680. handle_directive (ip, op)
  3681.      FILE_BUF *ip, *op;
  3682. {
  3683.   register U_CHAR *bp, *cp;
  3684.   register struct directive *kt;
  3685.   register int ident_length;
  3686.   U_CHAR *resume_p;
  3687.  
  3688.   /* Nonzero means we must copy the entire directive
  3689.      to get rid of comments or backslash-newlines.  */
  3690.   int copy_directive = 0;
  3691.  
  3692.   U_CHAR *ident, *after_ident;
  3693.  
  3694.   bp = ip->bufp;
  3695.  
  3696.   /* Record where the directive started.  do_xifdef needs this.  */
  3697.   directive_start = bp - 1;
  3698.  
  3699.   /* Skip whitespace and \-newline.  */
  3700.   while (1) {
  3701.     if (is_hor_space[*bp]) {
  3702.       if (*bp != ' ' && *bp != '\t' && pedantic)
  3703.     pedwarn ("%s in preprocessing directive", char_name[*bp]);
  3704.       bp++;
  3705.     } else if (*bp == '/' && (bp[1] == '*'
  3706.                   || (cplusplus_comments && bp[1] == '/'))) {
  3707.       ip->bufp = bp + 2;
  3708.       skip_to_end_of_comment (ip, &ip->lineno, 0);
  3709.       bp = ip->bufp;
  3710.     } else if (*bp == '\\' && bp[1] == '\n') {
  3711.       bp += 2; ip->lineno++;
  3712.     } else break;
  3713.   }
  3714.  
  3715.   /* Now find end of directive name.
  3716.      If we encounter a backslash-newline, exchange it with any following
  3717.      symbol-constituents so that we end up with a contiguous name.  */
  3718.  
  3719.   cp = bp;
  3720.   while (1) {
  3721.     if (is_idchar[*cp])
  3722.       cp++;
  3723.     else {
  3724.       if (*cp == '\\' && cp[1] == '\n')
  3725.     name_newline_fix (cp);
  3726.       if (is_idchar[*cp])
  3727.     cp++;
  3728.       else break;
  3729.     }
  3730.   }
  3731.   ident_length = cp - bp;
  3732.   ident = bp;
  3733.   after_ident = cp;
  3734.  
  3735.   /* A line of just `#' becomes blank.  */
  3736.  
  3737.   if (ident_length == 0 && *after_ident == '\n') {
  3738.     ip->bufp = after_ident;
  3739.     return 1;
  3740.   }
  3741.  
  3742.   if (ident_length == 0 || !is_idstart[*ident]) {
  3743.     U_CHAR *p = ident;
  3744.     while (is_idchar[*p]) {
  3745.       if (*p < '0' || *p > '9')
  3746.     break;
  3747.       p++;
  3748.     }
  3749.     /* Handle # followed by a line number.  */
  3750.     if (p != ident && !is_idchar[*p]) {
  3751.       static struct directive line_directive_table[] = {
  3752.     {  4, do_line, "line", T_LINE},
  3753.       };
  3754.       if (pedantic)
  3755.     pedwarn ("`#' followed by integer");
  3756.       after_ident = ident;
  3757.       kt = line_directive_table;
  3758.       goto old_linenum;
  3759.     }
  3760.  
  3761.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  3762.     if (p == ident) {
  3763.       while (*p == '#' || is_hor_space[*p]) p++;
  3764.       if (*p == '\n') {
  3765.     if (pedantic && !lang_asm)
  3766.       warning ("invalid preprocessing directive");
  3767.     return 0;
  3768.       }
  3769.     }
  3770.  
  3771.     if (!lang_asm)
  3772.       error ("invalid preprocessing directive name");
  3773.  
  3774.     return 0;
  3775.   }
  3776.  
  3777.   /*
  3778.    * Decode the keyword and call the appropriate expansion
  3779.    * routine, after moving the input pointer up to the next line.
  3780.    */
  3781.   for (kt = directive_table; kt->length > 0; kt++) {
  3782.     if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
  3783.       register U_CHAR *buf;
  3784.       register U_CHAR *limit;
  3785.       int unterminated;
  3786.       int junk;
  3787.       int *already_output;
  3788.  
  3789.       /* Nonzero means do not delete comments within the directive.
  3790.      #define needs this when -traditional.  */
  3791.       int keep_comments;
  3792.  
  3793.     old_linenum:
  3794.  
  3795.       limit = ip->buf + ip->length;
  3796.       unterminated = 0;
  3797.       already_output = 0;
  3798.       keep_comments = traditional && kt->traditional_comments;
  3799.       /* #import is defined only in Objective C, or when on the NeXT.  */
  3800.       if (kt->type == T_IMPORT
  3801.       && !(objc || lookup ((U_CHAR *) "__NeXT__", -1, -1)))
  3802.     break;
  3803.  
  3804.       /* Find the end of this directive (first newline not backslashed
  3805.      and not in a string or comment).
  3806.      Set COPY_DIRECTIVE if the directive must be copied
  3807.      (it contains a backslash-newline or a comment).  */
  3808.  
  3809.       buf = bp = after_ident;
  3810.       while (bp < limit) {
  3811.     register U_CHAR c = *bp++;
  3812.     switch (c) {
  3813.     case '\\':
  3814.       if (bp < limit) {
  3815.         if (*bp == '\n') {
  3816.           ip->lineno++;
  3817.           copy_directive = 1;
  3818.           bp++;
  3819.         } else if (traditional)
  3820.           bp++;
  3821.       }
  3822.       break;
  3823.  
  3824.     case '\'':
  3825.     case '\"':
  3826.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_directive, &unterminated);
  3827.       /* Don't bother calling the directive if we already got an error
  3828.          message due to unterminated string.  Skip everything and pretend
  3829.          we called the directive.  */
  3830.       if (unterminated) {
  3831.         if (traditional) {
  3832.           /* Traditional preprocessing permits unterminated strings.  */
  3833.           ip->bufp = bp;
  3834.           goto endloop1;
  3835.         }
  3836.         ip->bufp = bp;
  3837.         return 1;
  3838.       }
  3839.       break;
  3840.  
  3841.       /* <...> is special for #include.  */
  3842.     case '<':
  3843.       if (!kt->angle_brackets)
  3844.         break;
  3845.       while (bp < limit && *bp != '>' && *bp != '\n') {
  3846.         if (*bp == '\\' && bp[1] == '\n') {
  3847.           ip->lineno++;
  3848.           copy_directive = 1;
  3849.           bp++;
  3850.         }
  3851.         bp++;
  3852.       }
  3853.       break;
  3854.  
  3855.     case '/':
  3856.       if (*bp == '\\' && bp[1] == '\n')
  3857.         newline_fix (bp);
  3858.       if (*bp == '*'
  3859.           || (cplusplus_comments && *bp == '/')) {
  3860.         U_CHAR *obp = bp - 1;
  3861.         ip->bufp = bp + 1;
  3862.         skip_to_end_of_comment (ip, &ip->lineno, 0);
  3863.         bp = ip->bufp;
  3864.         /* No need to copy the directive because of a comment at the end;
  3865.            just don't include the comment in the directive.  */
  3866. /* Start of code from future cccp.c added by AMB */
  3867.         if (!put_out_comments) {
  3868.           U_CHAR *p;
  3869.           for (p = bp;  *p == ' ' || *p == '\t';  p++)
  3870.         continue;
  3871.           if (*p == '\n') {
  3872.         bp = obp;
  3873.         goto endloop1;
  3874.           }
  3875.         }
  3876. /* End of code from future cccp.c added by AMB */
  3877.         /* Don't remove the comments if -traditional.  */
  3878.         if (! keep_comments)
  3879.           copy_directive++;
  3880.       }
  3881.       break;
  3882.  
  3883.     case '\f':
  3884.     case '\r':
  3885.     case '\v':
  3886.       if (pedantic)
  3887.         pedwarn ("%s in preprocessing directive", char_name[c]);
  3888.       break;
  3889.  
  3890.     case '\n':
  3891.       --bp;        /* Point to the newline */
  3892.       ip->bufp = bp;
  3893.       goto endloop1;
  3894.     }
  3895.       }
  3896.       ip->bufp = bp;
  3897.  
  3898.     endloop1:
  3899.       resume_p = ip->bufp;
  3900.       /* BP is the end of the directive.
  3901.      RESUME_P is the next interesting data after the directive.
  3902.      A comment may come between.  */
  3903.  
  3904.       /* If a directive should be copied through, and -E was given,
  3905.      pass it through before removing comments.  */
  3906.       if (!no_output && put_out_comments &&
  3907. /* Start new option added by AMB */
  3908.           (kt->pass_thru || (kt->type == T_INCLUDE && dump_includes))) {
  3909. /* End new option added by AMB */
  3910.         int len;
  3911.  
  3912.     /* Output directive name.  */
  3913.         check_expand (op, kt->length + 2);
  3914.     /* Make sure # is at the start of a line */
  3915.     if (op->bufp > op->buf && op->bufp[-1] != '\n') {
  3916.       op->lineno++;
  3917.       *op->bufp++ = '\n';
  3918.     }
  3919.         *op->bufp++ = '#';
  3920.         bcopy (kt->name, op->bufp, kt->length);
  3921.         op->bufp += kt->length;
  3922.  
  3923.     /* Output arguments.  */
  3924.     len = (bp - buf);
  3925.     check_expand (op, len);
  3926.     bcopy (buf, (char *) op->bufp, len);
  3927.     op->bufp += len;
  3928.     /* Take account of any (escaped) newlines just output.  */
  3929.     while (--len >= 0)
  3930.       if (buf[len] == '\n')
  3931.         op->lineno++;
  3932.  
  3933.     already_output = &junk;
  3934.       }                /* Don't we need a newline or #line? */
  3935.  
  3936.       if (copy_directive) {
  3937.     register U_CHAR *xp = buf;
  3938.     /* Need to copy entire directive into temp buffer before dispatching */
  3939.  
  3940.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for directive plus
  3941.                           some slop */
  3942.     buf = cp;
  3943.  
  3944.     /* Copy to the new buffer, deleting comments
  3945.        and backslash-newlines (and whitespace surrounding the latter).  */
  3946.  
  3947.     while (xp < bp) {
  3948.       register U_CHAR c = *xp++;
  3949.       *cp++ = c;
  3950.  
  3951.       switch (c) {
  3952.       case '\n':
  3953.         abort ();  /* A bare newline should never part of the line.  */
  3954.         break;
  3955.  
  3956.         /* <...> is special for #include.  */
  3957.       case '<':
  3958.         if (!kt->angle_brackets)
  3959.           break;
  3960.         while (xp < bp && c != '>') {
  3961.           c = *xp++;
  3962.           if (c == '\\' && xp < bp && *xp == '\n')
  3963.         xp++;
  3964.           else
  3965.         *cp++ = c;
  3966.         }
  3967.         break;
  3968.  
  3969.       case '\\':
  3970.         if (*xp == '\n') {
  3971.           xp++;
  3972.           cp--;
  3973.           if (cp != buf && is_hor_space[cp[-1]]) {
  3974.         while (cp - 1 != buf && is_hor_space[cp[-2]])
  3975.           cp--;
  3976.         SKIP_WHITE_SPACE (xp);
  3977.           } else if (is_hor_space[*xp]) {
  3978.         *cp++ = *xp++;
  3979.         SKIP_WHITE_SPACE (xp);
  3980.           }
  3981.         } else if (traditional && xp < bp) {
  3982.           *cp++ = *xp++;
  3983.         }
  3984.         break;
  3985.  
  3986.       case '\'':
  3987.       case '\"':
  3988.         {
  3989.           register U_CHAR *bp1
  3990.         = skip_quoted_string (xp - 1, bp, ip->lineno,
  3991.                       NULL_PTR, NULL_PTR, NULL_PTR);
  3992.           while (xp != bp1)
  3993.         if (*xp == '\\') {
  3994.           if (*++xp != '\n')
  3995.             *cp++ = '\\';
  3996.           else
  3997.             xp++;
  3998.         } else
  3999.           *cp++ = *xp++;
  4000.         }
  4001.         break;
  4002.  
  4003.       case '/':
  4004.         if (*xp == '*'
  4005.         || (cplusplus_comments && *xp == '/')) {
  4006.           ip->bufp = xp + 1;
  4007.           /* If we already copied the directive through,
  4008.          already_output != 0 prevents outputting comment now.  */
  4009.           skip_to_end_of_comment (ip, already_output, 0);
  4010.           if (keep_comments)
  4011.         while (xp != ip->bufp)
  4012.           *cp++ = *xp++;
  4013.           /* Delete or replace the slash.  */
  4014.           else if (traditional)
  4015.         cp--;
  4016.           else
  4017.         cp[-1] = ' ';
  4018.           xp = ip->bufp;
  4019.         }
  4020.       }
  4021.     }
  4022.  
  4023.     /* Null-terminate the copy.  */
  4024.  
  4025.     *cp = 0;
  4026.       } else
  4027.     cp = bp;
  4028.  
  4029.       ip->bufp = resume_p;
  4030.  
  4031.       /* Some directives should be written out for cc1 to process,
  4032.      just as if they were not defined.  And sometimes we're copying
  4033.      definitions through.  */
  4034.  
  4035.       if (!no_output && already_output == 0
  4036.       && (kt->pass_thru
  4037. /* Start new option added by AMB */
  4038.           || (kt->type == T_INCLUDE
  4039.           && dump_includes)
  4040. /* End new option added by AMB */
  4041.           || (kt->type == T_DEFINE
  4042.           && (dump_macros == dump_names
  4043.               || dump_macros == dump_definitions)))) {
  4044.         int len;
  4045.  
  4046.     /* Output directive name.  */
  4047.         check_expand (op, kt->length + 1);
  4048.         *op->bufp++ = '#';
  4049.         bcopy (kt->name, (char *) op->bufp, kt->length);
  4050.         op->bufp += kt->length;
  4051.  
  4052.     if (kt->pass_thru || dump_macros == dump_definitions
  4053. /* Start new option added by AMB */
  4054.           || dump_includes
  4055. /* End new option added by AMB */
  4056.             ) {
  4057.       /* Output arguments.  */
  4058.       len = (cp - buf);
  4059.       check_expand (op, len);
  4060.       bcopy (buf, (char *) op->bufp, len);
  4061.       op->bufp += len;
  4062.     } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
  4063.       U_CHAR *xp = buf;
  4064.       U_CHAR *yp;
  4065.       SKIP_WHITE_SPACE (xp);
  4066.       yp = xp;
  4067.       while (is_idchar[*xp]) xp++;
  4068.       len = (xp - yp);
  4069.       check_expand (op, len + 1);
  4070.       *op->bufp++ = ' ';
  4071.       bcopy (yp, op->bufp, len);
  4072.       op->bufp += len;
  4073.     }
  4074.       }                /* Don't we need a newline or #line? */
  4075.  
  4076.       /* Call the appropriate directive handler.  buf now points to
  4077.      either the appropriate place in the input buffer, or to
  4078.      the temp buffer if it was necessary to make one.  cp
  4079.      points to the first char after the contents of the (possibly
  4080.      copied) directive, in either case. */
  4081.       (*kt->func) (buf, cp, op, kt);
  4082.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  4083.  
  4084.       return 1;
  4085.     }
  4086.   }
  4087.  
  4088.   /* It is deliberate that we don't warn about undefined directives.
  4089.      That is the responsibility of cc1.  */
  4090.   return 0;
  4091. }
  4092.  
  4093. static struct tm *
  4094. timestamp ()
  4095. {
  4096.   static struct tm *timebuf;
  4097.   if (!timebuf) {
  4098.     time_t t = time ((time_t *)0);
  4099.     timebuf = localtime (&t);
  4100.   }
  4101.   return timebuf;
  4102. }
  4103.  
  4104. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  4105.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  4106.                 };
  4107.  
  4108. /*
  4109.  * expand things like __FILE__.  Place the expansion into the output
  4110.  * buffer *without* rescanning.
  4111.  */
  4112.  
  4113. static void
  4114. special_symbol (hp, op)
  4115.      HASHNODE *hp;
  4116.      FILE_BUF *op;
  4117. {
  4118.   char *buf;
  4119.   int i, len;
  4120.   int true_indepth;
  4121.   FILE_BUF *ip = NULL;
  4122.   struct tm *timebuf;
  4123.  
  4124.   int paren = 0;        /* For special `defined' keyword */
  4125.  
  4126.   if (pcp_outfile && pcp_inside_if
  4127.       && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
  4128.     error ("Predefined macro `%s' used inside `#if' during precompilation",
  4129.        hp->name);
  4130.     
  4131.   for (i = indepth; i >= 0; i--)
  4132.     if (instack[i].fname != NULL) {
  4133.       ip = &instack[i];
  4134.       break;
  4135.     }
  4136.   if (ip == NULL) {
  4137.     error ("cccp error: not in any file?!");
  4138.     return;            /* the show must go on */
  4139.   }
  4140.  
  4141.   switch (hp->type) {
  4142.   case T_FILE:
  4143.   case T_BASE_FILE:
  4144.     {
  4145.       char *string;
  4146.       if (hp->type == T_FILE)
  4147.     string = ip->nominal_fname;
  4148.       else
  4149.     string = instack[0].nominal_fname;
  4150.  
  4151.       if (string)
  4152.     {
  4153.       buf = (char *) alloca (3 + 4 * strlen (string));
  4154.       quote_string (buf, string);
  4155.     }
  4156.       else
  4157.     buf = "\"\"";
  4158.  
  4159.       break;
  4160.     }
  4161.  
  4162.   case T_INCLUDE_LEVEL:
  4163.     true_indepth = 0;
  4164.     for (i = indepth; i >= 0; i--)
  4165.       if (instack[i].fname != NULL)
  4166.         true_indepth++;
  4167.  
  4168.     buf = (char *) alloca (8);    /* Eight bytes ought to be more than enough */
  4169.     sprintf (buf, "%d", true_indepth - 1);
  4170.     break;
  4171.  
  4172.   case T_VERSION:
  4173.     buf = (char *) alloca (3 + strlen (version_string));
  4174.     sprintf (buf, "\"%s\"", version_string);
  4175.     break;
  4176.  
  4177. #ifndef NO_BUILTIN_SIZE_TYPE
  4178.   case T_SIZE_TYPE:
  4179.     buf = SIZE_TYPE;
  4180.     break;
  4181. #endif
  4182.  
  4183. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  4184.   case T_PTRDIFF_TYPE:
  4185.     buf = PTRDIFF_TYPE;
  4186.     break;
  4187. #endif
  4188.  
  4189.   case T_WCHAR_TYPE:
  4190.     buf = wchar_type;
  4191.     break;
  4192.  
  4193.   case T_USER_LABEL_PREFIX_TYPE:
  4194.     buf = USER_LABEL_PREFIX;
  4195.     break;
  4196.  
  4197.   case T_REGISTER_PREFIX_TYPE:
  4198.     buf = REGISTER_PREFIX;
  4199.     break;
  4200.  
  4201.   case T_IMMEDIATE_PREFIX_TYPE:
  4202.     buf = IMMEDIATE_PREFIX;
  4203.     break;
  4204.  
  4205.   case T_CONST:
  4206.     buf = hp->value.cpval;
  4207.     if (pcp_inside_if && pcp_outfile)
  4208.       /* Output a precondition for this macro use */
  4209.       fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
  4210.     break;
  4211.  
  4212.   case T_SPECLINE:
  4213.     buf = (char *) alloca (10);
  4214.     sprintf (buf, "%d", ip->lineno);
  4215.     break;
  4216.  
  4217.   case T_DATE:
  4218.   case T_TIME:
  4219.     buf = (char *) alloca (20);
  4220.     timebuf = timestamp ();
  4221.     if (hp->type == T_DATE)
  4222.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  4223.           timebuf->tm_mday, timebuf->tm_year + 1900);
  4224.     else
  4225.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  4226.           timebuf->tm_sec);
  4227.     break;
  4228.  
  4229.   case T_SPEC_DEFINED:
  4230.     buf = " 0 ";        /* Assume symbol is not defined */
  4231.     ip = &instack[indepth];
  4232.     SKIP_WHITE_SPACE (ip->bufp);
  4233.     if (*ip->bufp == '(') {
  4234.       paren++;
  4235.       ip->bufp++;            /* Skip over the paren */
  4236.       SKIP_WHITE_SPACE (ip->bufp);
  4237.     }
  4238.  
  4239.     if (!is_idstart[*ip->bufp])
  4240.       goto oops;
  4241.     if ((hp = lookup (ip->bufp, -1, -1))) {
  4242.       if (pcp_outfile && pcp_inside_if
  4243.       && (hp->type == T_CONST
  4244.           || (hp->type == T_MACRO && hp->value.defn->predefined)))
  4245.     /* Output a precondition for this macro use. */
  4246.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  4247.       buf = " 1 ";
  4248.     }
  4249.     else
  4250.       if (pcp_outfile && pcp_inside_if)    {
  4251.     /* Output a precondition for this macro use */
  4252.     U_CHAR *cp = ip->bufp;
  4253.     fprintf (pcp_outfile, "#undef ");
  4254.     while (is_idchar[*cp]) /* Ick! */
  4255.       fputc (*cp++, pcp_outfile);
  4256.     putc ('\n', pcp_outfile);
  4257.       }
  4258.     while (is_idchar[*ip->bufp])
  4259.       ++ip->bufp;
  4260.     SKIP_WHITE_SPACE (ip->bufp);
  4261.     if (paren) {
  4262.       if (*ip->bufp != ')')
  4263.     goto oops;
  4264.       ++ip->bufp;
  4265.     }
  4266.     break;
  4267.  
  4268. oops:
  4269.  
  4270.     error ("`defined' without an identifier");
  4271.     break;
  4272.  
  4273.   default:
  4274.     error ("cccp error: invalid special hash type"); /* time for gdb */
  4275.     abort ();
  4276.   }
  4277.   len = strlen (buf);
  4278.   check_expand (op, len);
  4279.   bcopy (buf, (char *) op->bufp, len);
  4280.   op->bufp += len;
  4281.  
  4282.   return;
  4283. }
  4284.  
  4285.  
  4286. /* Routines to handle #directives */
  4287.  
  4288. /* Handle #include and #import.
  4289.    This function expects to see "fname" or <fname> on the input.  */
  4290.  
  4291. static int
  4292. do_include (buf, limit, op, keyword)
  4293.      U_CHAR *buf, *limit;
  4294.      FILE_BUF *op;
  4295.      struct directive *keyword;
  4296. {
  4297.   int importing = (keyword->type == T_IMPORT);
  4298.   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
  4299.   static int import_warning = 0;
  4300.   char *fname;        /* Dynamically allocated fname buffer */
  4301.   char *pcftry;
  4302.   char *pcfname;
  4303.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  4304.  
  4305.   struct file_name_list *search_start = include; /* Chain of dirs to search */
  4306.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  4307.   struct file_name_list *searchptr = 0;
  4308.   size_t flen;
  4309.  
  4310.   int f;            /* file number */
  4311.  
  4312.   int retried = 0;        /* Have already tried macro
  4313.                    expanding the include line*/
  4314.   int angle_brackets = 0;    /* 0 for "...", 1 for <...> */
  4315.   int pcf = -1;
  4316.   char *pcfbuf;
  4317.   char *pcfbuflimit;
  4318.   int pcfnum;
  4319.   f= -1;            /* JF we iz paranoid! */
  4320.  
  4321.   if (importing && warn_import && !inhibit_warnings
  4322.       && !instack[indepth].system_header_p && !import_warning) {
  4323.     import_warning = 1;
  4324.     warning ("using `#import' is not recommended");
  4325.     fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
  4326.     fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
  4327.     fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
  4328.     fprintf (stderr, "  #ifndef _FOO_H_INCLUDED\n");
  4329.     fprintf (stderr, "  #define _FOO_H_INCLUDED\n");
  4330.     fprintf (stderr, "  ... <real contents of file> ...\n");
  4331.     fprintf (stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
  4332.     fprintf (stderr, "Then users can use `#include' any number of times.\n");
  4333.     fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
  4334.     fprintf (stderr, "when it is equipped with such a conditional.\n");
  4335.   }
  4336.  
  4337. get_filename:
  4338.  
  4339.   fbeg = buf;
  4340.   SKIP_WHITE_SPACE (fbeg);
  4341.   /* Discard trailing whitespace so we can easily see
  4342.      if we have parsed all the significant chars we were given.  */
  4343.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  4344.  
  4345.   switch (*fbeg++) {
  4346.   case '\"':
  4347.     {
  4348.       FILE_BUF *fp;
  4349.       /* Copy the operand text, concatenating the strings.  */
  4350.       {
  4351.     U_CHAR *fin = fbeg;
  4352.     fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
  4353.     fend = fbeg;
  4354.     while (fin != limit) {
  4355.       while (fin != limit && *fin != '\"')
  4356.         *fend++ = *fin++;
  4357.       fin++;
  4358.       if (fin == limit)
  4359.         break;
  4360.       /* If not at the end, there had better be another string.  */
  4361.       /* Skip just horiz space, and don't go past limit.  */
  4362.       while (fin != limit && is_hor_space[*fin]) fin++;
  4363.       if (fin != limit && *fin == '\"')
  4364.         fin++;
  4365.       else
  4366.         goto fail;
  4367.     }
  4368.       }
  4369.       *fend = 0;
  4370.  
  4371.       /* We have "filename".  Figure out directory this source
  4372.      file is coming from and put it on the front of the list. */
  4373.  
  4374.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  4375.       if (ignore_srcdir) break;
  4376.  
  4377.       for (fp = &instack[indepth]; fp >= instack; fp--)
  4378.     {
  4379.       int n;
  4380.       char *ep,*nam;
  4381.  
  4382.       if ((nam = fp->nominal_fname) != NULL) {
  4383.         /* Found a named file.  Figure out dir of the file,
  4384.            and put it in front of the search list.  */
  4385.         dsp[0].next = search_start;
  4386.         search_start = dsp;
  4387. #ifndef VMS
  4388.         ep = rindex (nam, '/');
  4389. #ifdef DIR_SEPARATOR
  4390.         if (ep == NULL) ep = rindex (nam, DIR_SEPARATOR);
  4391.         else {
  4392.           char *tmp = rindex (nam, DIR_SEPARATOR);
  4393.           if (tmp != NULL && tmp > ep) ep = tmp;
  4394.         }
  4395. #endif
  4396. #else                /* VMS */
  4397.         ep = rindex (nam, ']');
  4398.         if (ep == NULL) ep = rindex (nam, '>');
  4399.         if (ep == NULL) ep = rindex (nam, ':');
  4400.         if (ep != NULL) ep++;
  4401. #endif                /* VMS */
  4402.         if (ep != NULL) {
  4403.           n = ep - nam;
  4404.           dsp[0].fname = (char *) alloca (n + 1);
  4405.           strncpy (dsp[0].fname, nam, n);
  4406.           dsp[0].fname[n] = '\0';
  4407.           if (n + INCLUDE_LEN_FUDGE > max_include_len)
  4408.         max_include_len = n + INCLUDE_LEN_FUDGE;
  4409.         } else {
  4410.           dsp[0].fname = 0; /* Current directory */
  4411.         }
  4412.         dsp[0].got_name_map = 0;
  4413.         break;
  4414.       }
  4415.     }
  4416.       break;
  4417.     }
  4418.  
  4419.   case '<':
  4420.     fend = fbeg;
  4421.     while (fend != limit && *fend != '>') fend++;
  4422.     if (*fend == '>' && fend + 1 == limit) {
  4423.       angle_brackets = 1;
  4424.       /* If -I-, start with the first -I dir after the -I-.  */
  4425.       if (first_bracket_include)
  4426.     search_start = first_bracket_include;
  4427.       break;
  4428.     }
  4429.     goto fail;
  4430.  
  4431.   default:
  4432. #ifdef VMS
  4433.     /*
  4434.      * Support '#include xyz' like VAX-C to allow for easy use of all the
  4435.      * decwindow include files. It defaults to '#include <xyz.h>' (so the
  4436.      * code from case '<' is repeated here) and generates a warning.
  4437.      * (Note: macro expansion of `xyz' takes precedence.)
  4438.      */
  4439.     if (retried && isalpha(*(--fbeg))) {
  4440.       fend = fbeg;
  4441.       while (fend != limit && (!isspace(*fend))) fend++;
  4442.       warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
  4443.       if (fend  == limit) {
  4444.     angle_brackets = 1;
  4445.     /* If -I-, start with the first -I dir after the -I-.  */
  4446.     if (first_bracket_include)
  4447.       search_start = first_bracket_include;
  4448.     break;
  4449.       }
  4450.     }
  4451. #endif
  4452.  
  4453.   fail:
  4454.     if (retried) {
  4455.       error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
  4456.       return 0;
  4457.     } else {
  4458.       /* Expand buffer and then remove any newline markers.
  4459.      We can't just tell expand_to_temp_buffer to omit the markers,
  4460.      since it would put extra spaces in include file names.  */
  4461.       FILE_BUF trybuf;
  4462.       U_CHAR *src;
  4463.       trybuf = expand_to_temp_buffer (buf, limit, 1, 0);
  4464.       src = trybuf.buf;
  4465.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  4466.       limit = buf;
  4467.       while (src != trybuf.bufp) {
  4468.     switch ((*limit++ = *src++)) {
  4469.       case '\n':
  4470.         limit--;
  4471.         src++;
  4472.         break;
  4473.  
  4474.       case '\'':
  4475.       case '\"':
  4476.         {
  4477.           U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0,
  4478.                          NULL_PTR, NULL_PTR, NULL_PTR);
  4479.           while (src != src1)
  4480.         *limit++ = *src++;
  4481.         }
  4482.         break;
  4483.     }
  4484.       }
  4485.       *limit = 0;
  4486.       free (trybuf.buf);
  4487.       retried++;
  4488.       goto get_filename;
  4489.     }
  4490.   }
  4491.  
  4492.   /* For #include_next, skip in the search path
  4493.      past the dir in which the containing file was found.  */
  4494.   if (skip_dirs) {
  4495.     FILE_BUF *fp;
  4496.     for (fp = &instack[indepth]; fp >= instack; fp--)
  4497.       if (fp->fname != NULL) {
  4498.     /* fp->dir is null if the containing file was specified
  4499.        with an absolute file name.  In that case, don't skip anything.  */
  4500.     if (fp->dir)
  4501.       search_start = fp->dir->next;
  4502.     break;
  4503.       }
  4504.   }
  4505.  
  4506.   flen = fend - fbeg;
  4507.  
  4508.   if (flen == 0)
  4509.     {
  4510.       error ("empty file name in `#%s'", keyword->name);
  4511.       return 0;
  4512.     }
  4513.  
  4514.   /* Allocate this permanently, because it gets stored in the definitions
  4515.      of macros.  */
  4516.   fname = xmalloc (max_include_len + flen + 4);
  4517.   /* + 2 above for slash and terminating null.  */
  4518.   /* + 2 added for '.h' on VMS (to support '#include filename') */
  4519.  
  4520.   /* If specified file name is absolute, just open it.  */
  4521.  
  4522.   if (*fbeg == '/'
  4523. #ifdef DIR_SEPARATOR
  4524.       || *fbeg == DIR_SEPARATOR
  4525. #endif
  4526.       ) {
  4527.     strncpy (fname, (char *) fbeg, flen);
  4528.     fname[flen] = 0;
  4529.     if (redundant_include_p (fname))
  4530.       return 0;
  4531.     if (importing)
  4532.       f = lookup_import (fname, NULL_PTR);
  4533.     else
  4534.       f = open_include_file (fname, NULL_PTR);
  4535.     if (f == -2)
  4536.       return 0;        /* Already included this file */
  4537.   } else {
  4538.     /* Search directory path, trying to open the file.
  4539.        Copy each filename tried into FNAME.  */
  4540.  
  4541.     for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
  4542.       if (searchptr->fname) {
  4543.     /* The empty string in a search path is ignored.
  4544.        This makes it possible to turn off entirely
  4545.        a standard piece of the list.  */
  4546.     if (searchptr->fname[0] == 0)
  4547.       continue;
  4548.     strcpy (fname, skip_redundant_dir_prefix (searchptr->fname));
  4549.     if (fname[0] && fname[strlen (fname) - 1] != '/')
  4550.       strcat (fname, "/");
  4551.       } else {
  4552.     fname[0] = 0;
  4553.       }
  4554.       strncat (fname, (char *) fbeg, flen);
  4555. #ifdef VMS
  4556.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  4557.          full VMS file specification */
  4558.       if (searchptr->fname && (searchptr->fname[0] != 0)) {
  4559.     /* Fix up the filename */
  4560.     hack_vms_include_specification (fname);
  4561.       } else {
  4562.           /* This is a normal VMS filespec, so use it unchanged.  */
  4563.     strncpy (fname, fbeg, flen);
  4564.     fname[flen] = 0;
  4565.     /* if it's '#include filename', add the missing .h */
  4566.     if (index(fname,'.')==NULL) {
  4567.       strcat (fname, ".h");
  4568.     }
  4569.       }
  4570. #endif /* VMS */
  4571.       /* ??? There are currently 3 separate mechanisms for avoiding processing
  4572.      of redundant include files: #import, #pragma once, and
  4573.      redundant_include_p.  It would be nice if they were unified.  */
  4574.       if (redundant_include_p (fname))
  4575.     return 0;
  4576.       if (importing)
  4577.     f = lookup_import (fname, searchptr);
  4578.       else
  4579.     f = open_include_file (fname, searchptr);
  4580.       if (f == -2)
  4581.     return 0;            /* Already included this file */
  4582. #ifdef EACCES
  4583.       else if (f == -1 && errno == EACCES)
  4584.     warning ("Header file %s exists, but is not readable", fname);
  4585. #endif
  4586.       if (f >= 0)
  4587.     break;
  4588.     }
  4589.   }
  4590.  
  4591.   if (f < 0) {
  4592.     /* A file that was not found.  */
  4593.  
  4594.     strncpy (fname, (char *) fbeg, flen);
  4595.     fname[flen] = 0;
  4596.     /* If generating dependencies and -MG was specified, we assume missing
  4597.        files are leaf files, living in the same directory as the source file
  4598.        or other similar place; these missing files may be generated from
  4599.        other files and may not exist yet (eg: y.tab.h).  */
  4600.     if (print_deps_missing_files
  4601.     && print_deps > (angle_brackets || (system_include_depth > 0)))
  4602.       {
  4603.     /* If it was requested as a system header file,
  4604.        then assume it belongs in the first place to look for such.  */
  4605.     if (angle_brackets)
  4606.       {
  4607.         for (searchptr = search_start; searchptr; searchptr = searchptr->next)
  4608.           {
  4609.         if (searchptr->fname)
  4610.           {
  4611.             char *p;
  4612.  
  4613.             if (searchptr->fname[0] == 0)
  4614.               continue;
  4615.             p = (char *) alloca (strlen (searchptr->fname)
  4616.                      + strlen (fname) + 2);
  4617.             strcpy (p, skip_redundant_dir_prefix (searchptr->fname));
  4618.             if (p[0] && p[strlen (p) - 1] != '/')
  4619.               strcat (p, "/");
  4620.             strcat (p, fname);
  4621.             deps_output (p, ' ');
  4622.             break;
  4623.           }
  4624.           }
  4625.       }
  4626.     else
  4627.       {
  4628.         /* Otherwise, omit the directory, as if the file existed
  4629.            in the directory with the source.  */
  4630.         deps_output (fname, ' ');
  4631.       }
  4632.       }
  4633.     /* If -M was specified, and this header file won't be added to the
  4634.        dependency list, then don't count this as an error, because we can
  4635.        still produce correct output.  Otherwise, we can't produce correct
  4636.        output, because there may be dependencies we need inside the missing
  4637.        file, and we don't know what directory this missing file exists in.  */
  4638.     else if (print_deps
  4639.     && (print_deps <= (angle_brackets || (system_include_depth > 0))))
  4640.       warning ("No include path in which to find %s", fname);
  4641.     else if (search_start)
  4642.       error_from_errno (fname);
  4643.     else
  4644.       error ("No include path in which to find %s", fname);
  4645.   } else {
  4646.     /* Check to see if this include file is a once-only include file.
  4647.        If so, give up.  */
  4648.  
  4649.     struct file_name_list* ptr;
  4650.  
  4651.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  4652.       if (!strcmp (ptr->fname, fname)) {
  4653.     close (f);
  4654.         return 0;                /* This file was once'd. */
  4655.       }
  4656.     }
  4657.  
  4658.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  4659.       if (!strcmp (ptr->fname, fname))
  4660.         break;                /* This file was included before. */
  4661.     }
  4662.  
  4663.     if (ptr == 0) {
  4664.       /* This is the first time for this file.  */
  4665.       /* Add it to list of files included.  */
  4666.  
  4667.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  4668.       ptr->control_macro = 0;
  4669.       ptr->c_system_include_path = 0;
  4670.       ptr->next = all_include_files;
  4671.       all_include_files = ptr;
  4672.       ptr->fname = savestring (fname);
  4673.       ptr->got_name_map = 0;
  4674.  
  4675.       /* For -M, add this file to the dependencies.  */
  4676.       if (print_deps > (angle_brackets || (system_include_depth > 0)))
  4677.     deps_output (fname, ' ');
  4678.     }   
  4679.  
  4680.     /* Handle -H option.  */
  4681.     if (print_include_names)
  4682.       fprintf (stderr, "%*s%s\n", indepth, "", fname);
  4683.  
  4684.     if (angle_brackets)
  4685.       system_include_depth++;
  4686.  
  4687.     /* Actually process the file.  */
  4688.     add_import (f, fname);    /* Record file on "seen" list for #import. */
  4689.  
  4690.     pcftry = (char *) alloca (strlen (fname) + 30);
  4691.     pcfbuf = 0;
  4692.     pcfnum = 0;
  4693.  
  4694.     if (!no_precomp)
  4695.       {
  4696.     struct stat stat_f;
  4697.  
  4698.     fstat (f, &stat_f);
  4699.  
  4700.     do {
  4701.       sprintf (pcftry, "%s%d", fname, pcfnum++);
  4702.  
  4703.       pcf = open (pcftry, O_RDONLY, 0666);
  4704.       if (pcf != -1)
  4705.         {
  4706.           struct stat s;
  4707.  
  4708.           fstat (pcf, &s);
  4709.           if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
  4710.             sizeof (s.st_ino))
  4711.           || stat_f.st_dev != s.st_dev)
  4712.         {
  4713.           pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
  4714.           /* Don't need it any more.  */
  4715.           close (pcf);
  4716.         }
  4717.           else
  4718.         {
  4719.           /* Don't need it at all.  */
  4720.           close (pcf);
  4721.           break;
  4722.         }
  4723.         }
  4724.     } while (pcf != -1 && !pcfbuf);
  4725.       }
  4726.     
  4727.     /* Actually process the file */
  4728.     if (pcfbuf) {
  4729.       pcfname = xmalloc (strlen (pcftry) + 1);
  4730.       strcpy (pcfname, pcftry);
  4731.       pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) pcfbuflimit,
  4732.           (U_CHAR *) fname, op);
  4733.     }
  4734.     else
  4735.       finclude (f, fname, op, is_system_include (fname), searchptr);
  4736.  
  4737.     if (angle_brackets)
  4738.       system_include_depth--;
  4739.   }
  4740.   return 0;
  4741. }
  4742.  
  4743. /* Return nonzero if there is no need to include file NAME
  4744.    because it has already been included and it contains a conditional
  4745.    to make a repeated include do nothing.  */
  4746.  
  4747. static int
  4748. redundant_include_p (name)
  4749.      char *name;
  4750. {
  4751.   struct file_name_list *l = all_include_files;
  4752.   for (; l; l = l->next)
  4753.     if (! strcmp (name, l->fname)
  4754.     && l->control_macro
  4755.     && lookup (l->control_macro, -1, -1))
  4756.       return 1;
  4757.   return 0;
  4758. }
  4759.  
  4760. /* Return nonzero if the given FILENAME is an absolute pathname which
  4761.    designates a file within one of the known "system" include file
  4762.    directories.  We assume here that if the given FILENAME looks like
  4763.    it is the name of a file which resides either directly in a "system"
  4764.    include file directory, or within any subdirectory thereof, then the
  4765.    given file must be a "system" include file.  This function tells us
  4766.    if we should suppress pedantic errors/warnings for the given FILENAME.
  4767.  
  4768.    The value is 2 if the file is a C-language system header file
  4769.    for which C++ should (on most systems) assume `extern "C"'.  */
  4770.  
  4771. static int
  4772. is_system_include (filename)
  4773.     register char *filename;
  4774. {
  4775.   struct file_name_list *searchptr;
  4776.  
  4777.   for (searchptr = first_system_include; searchptr;
  4778.        searchptr = searchptr->next)
  4779.     if (searchptr->fname) {
  4780.       register char *sys_dir = skip_redundant_dir_prefix (searchptr->fname);
  4781.       register unsigned length = strlen (sys_dir);
  4782.  
  4783.       if (! strncmp (sys_dir, filename, length)
  4784.       && (filename[length] == '/'
  4785. #ifdef DIR_SEPARATOR
  4786.           || filename[length] == DIR_SEPARATOR
  4787. #endif
  4788.           )) {
  4789.     if (searchptr->c_system_include_path)
  4790.       return 2;
  4791.     else
  4792.       return 1;
  4793.       }
  4794.     }
  4795.   return 0;
  4796. }
  4797.  
  4798. /* Skip leading "./" from a directory name.
  4799.    This may yield the empty string, which represents the current directory.  */
  4800.  
  4801. static char *
  4802. skip_redundant_dir_prefix (dir)
  4803.      char *dir;
  4804. {
  4805.   while (dir[0] == '.' && dir[1] == '/')
  4806.     for (dir += 2; *dir == '/'; dir++)
  4807.       continue;
  4808.   if (dir[0] == '.' && !dir[1])
  4809.     dir++;
  4810.   return dir;
  4811. }
  4812.  
  4813. /* The file_name_map structure holds a mapping of file names for a
  4814.    particular directory.  This mapping is read from the file named
  4815.    FILE_NAME_MAP_FILE in that directory.  Such a file can be used to
  4816.    map filenames on a file system with severe filename restrictions,
  4817.    such as DOS.  The format of the file name map file is just a series
  4818.    of lines with two tokens on each line.  The first token is the name
  4819.    to map, and the second token is the actual name to use.  */
  4820.  
  4821. struct file_name_map
  4822. {
  4823.   struct file_name_map *map_next;
  4824.   char *map_from;
  4825.   char *map_to;
  4826. };
  4827.  
  4828. #define FILE_NAME_MAP_FILE "header.gcc"
  4829.  
  4830. /* Read a space delimited string of unlimited length from a stdio
  4831.    file.  */
  4832.  
  4833. static char *
  4834. read_filename_string (ch, f)
  4835.      int ch;
  4836.      FILE *f;
  4837. {
  4838.   char *alloc, *set;
  4839.   int len;
  4840.  
  4841.   len = 20;
  4842.   set = alloc = xmalloc (len + 1);
  4843.   if (! is_space[ch])
  4844.     {
  4845.       *set++ = ch;
  4846.       while ((ch = getc (f)) != EOF && ! is_space[ch])
  4847.     {
  4848.       if (set - alloc == len)
  4849.         {
  4850.           len *= 2;
  4851.           alloc = xrealloc (alloc, len + 1);
  4852.           set = alloc + len / 2;
  4853.         }
  4854.       *set++ = ch;
  4855.     }
  4856.     }
  4857.   *set = '\0';
  4858.   ungetc (ch, f);
  4859.   return alloc;
  4860. }
  4861.  
  4862. /* Read the file name map file for DIRNAME.  */
  4863.  
  4864. static struct file_name_map *
  4865. read_name_map (dirname)
  4866.      char *dirname;
  4867. {
  4868.   /* This structure holds a linked list of file name maps, one per
  4869.      directory.  */
  4870.   struct file_name_map_list
  4871.     {
  4872.       struct file_name_map_list *map_list_next;
  4873.       char *map_list_name;
  4874.       struct file_name_map *map_list_map;
  4875.     };
  4876.   static struct file_name_map_list *map_list;
  4877.   register struct file_name_map_list *map_list_ptr;
  4878.   char *name;
  4879.   FILE *f;
  4880.   size_t dirlen;
  4881.   int separator_needed;
  4882.  
  4883.   dirname = skip_redundant_dir_prefix (dirname);
  4884.  
  4885.   for (map_list_ptr = map_list; map_list_ptr;
  4886.        map_list_ptr = map_list_ptr->map_list_next)
  4887.     if (! strcmp (map_list_ptr->map_list_name, dirname))
  4888.       return map_list_ptr->map_list_map;
  4889.  
  4890.   map_list_ptr = ((struct file_name_map_list *)
  4891.           xmalloc (sizeof (struct file_name_map_list)));
  4892.   map_list_ptr->map_list_name = savestring (dirname);
  4893.   map_list_ptr->map_list_map = NULL;
  4894.  
  4895.   dirlen = strlen (dirname);
  4896.   separator_needed = dirlen != 0 && dirname[dirlen - 1] != '/';
  4897.   name = (char *) alloca (dirlen + strlen (FILE_NAME_MAP_FILE) + 2);
  4898.   strcpy (name, dirname);
  4899.   name[dirlen] = '/';
  4900.   strcpy (name + dirlen + separator_needed, FILE_NAME_MAP_FILE);
  4901.   f = fopen (name, "r");
  4902.   if (!f)
  4903.     map_list_ptr->map_list_map = NULL;
  4904.   else
  4905.     {
  4906.       int ch;
  4907.  
  4908.       while ((ch = getc (f)) != EOF)
  4909.     {
  4910.       char *from, *to;
  4911.       struct file_name_map *ptr;
  4912.  
  4913.       if (is_space[ch])
  4914.         continue;
  4915.       from = read_filename_string (ch, f);
  4916.       while ((ch = getc (f)) != EOF && is_hor_space[ch])
  4917.         ;
  4918.       to = read_filename_string (ch, f);
  4919.  
  4920.       ptr = ((struct file_name_map *)
  4921.          xmalloc (sizeof (struct file_name_map)));
  4922.       ptr->map_from = from;
  4923.  
  4924.       /* Make the real filename absolute.  */
  4925.       if (*to == '/')
  4926.         ptr->map_to = to;
  4927.       else
  4928.         {
  4929.           ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
  4930.           strcpy (ptr->map_to, dirname);
  4931.           ptr->map_to[dirlen] = '/';
  4932.           strcpy (ptr->map_to + dirlen + separator_needed, to);
  4933.           free (to);
  4934.         }          
  4935.  
  4936.       ptr->map_next = map_list_ptr->map_list_map;
  4937.       map_list_ptr->map_list_map = ptr;
  4938.  
  4939.       while ((ch = getc (f)) != '\n')
  4940.         if (ch == EOF)
  4941.           break;
  4942.     }
  4943.       fclose (f);
  4944.     }
  4945.   
  4946.   map_list_ptr->map_list_next = map_list;
  4947.   map_list = map_list_ptr;
  4948.  
  4949.   return map_list_ptr->map_list_map;
  4950. }  
  4951.  
  4952. /* Try to open include file FILENAME.  SEARCHPTR is the directory
  4953.    being tried from the include file search path.  This function maps
  4954.    filenames on file systems based on information read by
  4955.    read_name_map.  */
  4956.  
  4957. static int
  4958. open_include_file (filename, searchptr)
  4959.      char *filename;
  4960.      struct file_name_list *searchptr;
  4961. {
  4962.   register struct file_name_map *map;
  4963.   register char *from;
  4964.   char *p, *dir;
  4965.  
  4966.   if (searchptr && ! searchptr->got_name_map)
  4967.     {
  4968.       searchptr->name_map = read_name_map (searchptr->fname
  4969.                        ? searchptr->fname : ".");
  4970.       searchptr->got_name_map = 1;
  4971.     }
  4972.  
  4973.   /* First check the mapping for the directory we are using.  */
  4974.   if (searchptr && searchptr->name_map)
  4975.     {
  4976.       from = filename;
  4977.       if (searchptr->fname)
  4978.     from += strlen (searchptr->fname) + 1;
  4979.       for (map = searchptr->name_map; map; map = map->map_next)
  4980.     {
  4981.       if (! strcmp (map->map_from, from))
  4982.         {
  4983.           /* Found a match.  */
  4984.           return open (map->map_to, O_RDONLY, 0666);
  4985.         }
  4986.     }
  4987.     }
  4988.  
  4989.   /* Try to find a mapping file for the particular directory we are
  4990.      looking in.  Thus #include <sys/types.h> will look up sys/types.h
  4991.      in /usr/include/header.gcc and look up types.h in
  4992.      /usr/include/sys/header.gcc.  */
  4993.   p = rindex (filename, '/');
  4994. #ifdef DIR_SEPARATOR
  4995.   if (! p) p = rindex (filename, DIR_SEPARATOR);
  4996.   else {
  4997.     char *tmp = rindex (filename, DIR_SEPARATOR);
  4998.     if (tmp != NULL && tmp > p) p = tmp;
  4999.   }
  5000. #endif
  5001.   if (! p)
  5002.     p = filename;
  5003.   if (searchptr
  5004.       && searchptr->fname
  5005.       && strlen (searchptr->fname) == p - filename
  5006.       && ! strncmp (searchptr->fname, filename, p - filename))
  5007.     {
  5008.       /* FILENAME is in SEARCHPTR, which we've already checked.  */
  5009.       return open (filename, O_RDONLY, 0666);
  5010.     }
  5011.  
  5012.   if (p == filename)
  5013.     {
  5014.       dir = ".";
  5015.       from = filename;
  5016.     }
  5017.   else
  5018.     {
  5019.       dir = (char *) alloca (p - filename + 1);
  5020.       bcopy (filename, dir, p - filename);
  5021.       dir[p - filename] = '\0';
  5022.       from = p + 1;
  5023.     }
  5024.   for (map = read_name_map (dir); map; map = map->map_next)
  5025.     if (! strcmp (map->map_from, from))
  5026.       return open (map->map_to, O_RDONLY, 0666);
  5027.  
  5028.   return open (filename, O_RDONLY, 0666);
  5029. }
  5030.  
  5031. /* Process the contents of include file FNAME, already open on descriptor F,
  5032.    with output to OP.
  5033.    SYSTEM_HEADER_P is 1 if this file resides in any one of the known
  5034.    "system" include directories (as decided by the `is_system_include'
  5035.    function above).
  5036.    DIRPTR is the link in the dir path through which this file was found,
  5037.    or 0 if the file name was absolute.  */
  5038.  
  5039. static void
  5040. finclude (f, fname, op, system_header_p, dirptr)
  5041.      int f;
  5042.      char *fname;
  5043.      FILE_BUF *op;
  5044.      int system_header_p;
  5045.      struct file_name_list *dirptr;
  5046. {
  5047.   int st_mode;
  5048.   long st_size;
  5049.   long i;
  5050.   FILE_BUF *fp;            /* For input stack frame */
  5051.   int missing_newline = 0;
  5052.  
  5053.   CHECK_DEPTH (return;);
  5054.  
  5055.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  5056.     {
  5057.       perror_with_name (fname);
  5058.       close (f);
  5059.       return;
  5060.     }
  5061.  
  5062.   fp = &instack[indepth + 1];
  5063.   bzero ((char *) fp, sizeof (FILE_BUF));
  5064.   fp->nominal_fname = fp->fname = fname;
  5065.   fp->length = 0;
  5066.   fp->lineno = 1;
  5067.   fp->if_stack = if_stack;
  5068.   fp->system_header_p = system_header_p;
  5069.   fp->dir = dirptr;
  5070.  
  5071.   if (S_ISREG (st_mode)) {
  5072.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  5073.     fp->bufp = fp->buf;
  5074.  
  5075.     /* Read the file contents, knowing that st_size is an upper bound
  5076.        on the number of bytes we can read.  */
  5077.     fp->length = safe_read (f, (char *) fp->buf, st_size);
  5078.     if (fp->length < 0) goto nope;
  5079.   }
  5080.   else if (S_ISDIR (st_mode)) {
  5081.     error ("directory `%s' specified in #include", fname);
  5082.     close (f);
  5083.     return;
  5084.   } else {
  5085.     /* Cannot count its file size before reading.
  5086.        First read the entire file into heap and
  5087.        copy them into buffer on stack. */
  5088.  
  5089.     int bsize = 2000;
  5090.  
  5091.     st_size = 0;
  5092.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  5093.  
  5094.     for (;;) {
  5095.       i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
  5096.       if (i < 0)
  5097.     goto nope;      /* error! */
  5098.       st_size += i;
  5099.       if (st_size != bsize)
  5100.     break;    /* End of file */
  5101.       bsize *= 2;
  5102.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  5103.     }
  5104.     fp->bufp = fp->buf;
  5105.     fp->length = st_size;
  5106.   }
  5107.  
  5108.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  5109.       /* Backslash-newline at end is not good enough.  */
  5110.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  5111.     fp->buf[fp->length++] = '\n';
  5112.     missing_newline = 1;
  5113.   }
  5114.   fp->buf[fp->length] = '\0';
  5115.  
  5116.   /* Close descriptor now, so nesting does not use lots of descriptors.  */
  5117.   close (f);
  5118.  
  5119.   /* Must do this before calling trigraph_pcp, so that the correct file name
  5120.      will be printed in warning messages.  */
  5121.  
  5122.   indepth++;
  5123.   input_file_stack_tick++;
  5124.  
  5125.   if (!no_trigraphs)
  5126.     trigraph_pcp (fp);
  5127.  
  5128.   output_line_directive (fp, op, 0, enter_file);
  5129.   rescan (op, 0);
  5130.  
  5131.   if (missing_newline)
  5132.     fp->lineno--;
  5133.  
  5134.   if (pedantic && missing_newline)
  5135.     pedwarn ("file does not end in newline");
  5136.  
  5137.   indepth--;
  5138.   input_file_stack_tick++;
  5139.   output_line_directive (&instack[indepth], op, 0, leave_file);
  5140.   free (fp->buf);
  5141.   return;
  5142.  
  5143.  nope:
  5144.  
  5145.   perror_with_name (fname);
  5146.   close (f);
  5147.   free (fp->buf);
  5148. }
  5149.  
  5150. /* Record that inclusion of the file named FILE
  5151.    should be controlled by the macro named MACRO_NAME.
  5152.    This means that trying to include the file again
  5153.    will do something if that macro is defined.  */
  5154.  
  5155. static void
  5156. record_control_macro (file, macro_name)
  5157.      char *file;
  5158.      U_CHAR *macro_name;
  5159. {
  5160.   struct file_name_list *new;
  5161.  
  5162.   for (new = all_include_files; new; new = new->next) {
  5163.     if (!strcmp (new->fname, file)) {
  5164.       new->control_macro = macro_name;
  5165.       return;
  5166.     }
  5167.   }
  5168.  
  5169.   /* If the file is not in all_include_files, something's wrong.  */
  5170.   abort ();
  5171. }
  5172.  
  5173. /* Maintain and search list of included files, for #import.  */
  5174.  
  5175. #define IMPORT_HASH_SIZE 31
  5176.  
  5177. struct import_file {
  5178.   char *name;
  5179.   ino_t inode;
  5180.   dev_t dev;
  5181.   struct import_file *next;
  5182. };
  5183.  
  5184. /* Hash table of files already included with #include or #import.  */
  5185.  
  5186. static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
  5187.  
  5188. /* Hash a file name for import_hash_table.  */
  5189.  
  5190. static int 
  5191. import_hash (f)
  5192.      char *f;
  5193. {
  5194.   int val = 0;
  5195.  
  5196.   while (*f) val += *f++;
  5197.   return (val%IMPORT_HASH_SIZE);
  5198. }
  5199.  
  5200. /* Search for file FILENAME in import_hash_table.
  5201.    Return -2 if found, either a matching name or a matching inode.
  5202.    Otherwise, open the file and return a file descriptor if successful
  5203.    or -1 if unsuccessful.  */
  5204.  
  5205. static int
  5206. lookup_import (filename, searchptr)
  5207.      char *filename;
  5208.      struct file_name_list *searchptr;
  5209. {
  5210.   struct import_file *i;
  5211.   int h;
  5212.   int hashval;
  5213.   struct stat sb;
  5214.   int fd;
  5215.  
  5216.   hashval = import_hash (filename);
  5217.  
  5218.   /* Attempt to find file in list of already included files */
  5219.   i = import_hash_table[hashval];
  5220.  
  5221.   while (i) {
  5222.     if (!strcmp (filename, i->name))
  5223.       return -2;        /* return found */
  5224.     i = i->next;
  5225.   }
  5226.   /* Open it and try a match on inode/dev */
  5227.   fd = open_include_file (filename, searchptr);
  5228.   if (fd < 0)
  5229.     return fd;
  5230.   fstat (fd, &sb);
  5231.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  5232.     i = import_hash_table[h];
  5233.     while (i) {
  5234.       /* Compare the inode and the device.
  5235.      Supposedly on some systems the inode is not a scalar.  */
  5236.       if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
  5237.       && i->dev == sb.st_dev) {
  5238.         close (fd);
  5239.         return -2;        /* return found */
  5240.       }
  5241.       i = i->next;
  5242.     }
  5243.   }
  5244.   return fd;            /* Not found, return open file */
  5245. }
  5246.  
  5247. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  5248.  
  5249. static void
  5250. add_import (fd, fname)
  5251.      int fd;
  5252.      char *fname;
  5253. {
  5254.   struct import_file *i;
  5255.   int hashval;
  5256.   struct stat sb;
  5257.  
  5258.   hashval = import_hash (fname);
  5259.   fstat (fd, &sb);
  5260.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  5261.   i->name = xmalloc (strlen (fname)+1);
  5262.   strcpy (i->name, fname);
  5263.   bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
  5264.   i->dev = sb.st_dev;
  5265.   i->next = import_hash_table[hashval];
  5266.   import_hash_table[hashval] = i;
  5267. }
  5268.  
  5269. /* Load the specified precompiled header into core, and verify its
  5270.    preconditions.  PCF indicates the file descriptor to read, which must
  5271.    be a regular file.  FNAME indicates the file name of the original 
  5272.    header.  *LIMIT will be set to an address one past the end of the file.
  5273.    If the preconditions of the file are not satisfied, the buffer is 
  5274.    freed and we return 0.  If the preconditions are satisfied, return
  5275.    the address of the buffer following the preconditions.  The buffer, in
  5276.    this case, should never be freed because various pieces of it will
  5277.    be referred to until all precompiled strings are output at the end of
  5278.    the run.
  5279. */
  5280. static char *
  5281. check_precompiled (pcf, fname, limit)
  5282.      int pcf;
  5283.      char *fname;
  5284.      char **limit;
  5285. {
  5286.   int st_mode;
  5287.   long st_size;
  5288.   int length = 0;
  5289.   char *buf;
  5290.   char *cp;
  5291.  
  5292.   if (pcp_outfile)
  5293.     return 0;
  5294.   
  5295.   if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
  5296.     return 0;
  5297.  
  5298.   if (S_ISREG (st_mode))
  5299.     {
  5300.       buf = xmalloc (st_size + 2);
  5301.       length = safe_read (pcf, buf, st_size);
  5302.       if (length < 0)
  5303.     goto nope;
  5304.     }
  5305.   else
  5306.     abort ();
  5307.     
  5308.   if (length > 0 && buf[length-1] != '\n')
  5309.     buf[length++] = '\n';
  5310.   buf[length] = '\0';
  5311.   
  5312.   *limit = buf + length;
  5313.  
  5314.   /* File is in core.  Check the preconditions. */
  5315.   if (!check_preconditions (buf))
  5316.     goto nope;
  5317.   for (cp = buf; *cp; cp++)
  5318.     ;
  5319. #ifdef DEBUG_PCP
  5320.   fprintf (stderr, "Using preinclude %s\n", fname);
  5321. #endif
  5322.   return cp + 1;
  5323.  
  5324.  nope:
  5325. #ifdef DEBUG_PCP
  5326.   fprintf (stderr, "Cannot use preinclude %s\n", fname);
  5327. #endif
  5328.   free (buf);
  5329.   return 0;
  5330. }
  5331.  
  5332. /* PREC (null terminated) points to the preconditions of a
  5333.    precompiled header.  These are a series of #define and #undef
  5334.    lines which must match the current contents of the hash
  5335.    table.  */
  5336. static int 
  5337. check_preconditions (prec)
  5338.      char *prec;
  5339. {
  5340.   MACRODEF mdef;
  5341.   char *lineend;
  5342.   
  5343.   while (*prec) {
  5344.     lineend = index (prec, '\n');
  5345.     
  5346.     if (*prec++ != '#') {
  5347.       error ("Bad format encountered while reading precompiled file");
  5348.       return 0;
  5349.     }
  5350.     if (!strncmp (prec, "define", 6)) {
  5351.       HASHNODE *hp;
  5352.       
  5353.       prec += 6;
  5354.       mdef = create_definition ((U_CHAR *) prec, (U_CHAR *) lineend, NULL_PTR);
  5355.  
  5356.       if (mdef.defn == 0)
  5357.     abort ();
  5358.       
  5359.       if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
  5360.       || (hp->type != T_MACRO && hp->type != T_CONST)
  5361.       || (hp->type == T_MACRO
  5362.           && !compare_defs (mdef.defn, hp->value.defn)
  5363.           && (mdef.defn->length != 2
  5364.           || mdef.defn->expansion[0] != '\n'
  5365.           || mdef.defn->expansion[1] != ' ')))
  5366.     return 0;
  5367.     } else if (!strncmp (prec, "undef", 5)) {
  5368.       char *name;
  5369.       int len;
  5370.       
  5371.       prec += 5;
  5372.       while (is_hor_space[(U_CHAR) *prec])
  5373.     prec++;
  5374.       name = prec;
  5375.       while (is_idchar[(U_CHAR) *prec])
  5376.     prec++;
  5377.       len = prec - name;
  5378.       
  5379.       if (lookup ((U_CHAR *) name, len, -1))
  5380.     return 0;
  5381.     } else {
  5382.       error ("Bad format encountered while reading precompiled file");
  5383.       return 0;
  5384.     }
  5385.     prec = lineend + 1;
  5386.   }
  5387.   /* They all passed successfully */
  5388.   return 1;
  5389. }
  5390.  
  5391. /* Process the main body of a precompiled file.  BUF points to the
  5392.    string section of the file, following the preconditions.  LIMIT is one
  5393.    character past the end.  NAME is the name of the file being read
  5394.    in.  OP is the main output buffer */
  5395. static void
  5396. pcfinclude (buf, limit, name, op)
  5397.      U_CHAR *buf, *limit, *name;
  5398.      FILE_BUF *op;
  5399. {
  5400.   FILE_BUF tmpbuf;
  5401.   int nstrings;
  5402.   U_CHAR *cp = buf;
  5403.  
  5404.   /* First in the file comes 4 bytes indicating the number of strings, */
  5405.   /* in network byte order. (MSB first).  */
  5406.   nstrings = *cp++;
  5407.   nstrings = (nstrings << 8) | *cp++;
  5408.   nstrings = (nstrings << 8) | *cp++;
  5409.   nstrings = (nstrings << 8) | *cp++;
  5410.   
  5411.   /* Looping over each string... */
  5412.   while (nstrings--) {
  5413.     U_CHAR *string_start;
  5414.     U_CHAR *endofthiskey;
  5415.     STRINGDEF *str;
  5416.     int nkeys;
  5417.     
  5418.     /* Each string starts with a STRINGDEF structure (str), followed */
  5419.     /* by the text of the string (string_start) */
  5420.  
  5421.     /* First skip to a longword boundary */
  5422.     /* ??? Why a 4-byte boundary?  On all machines? */
  5423.     /* NOTE: This works correctly even if HOST_WIDE_INT
  5424.        is narrower than a pointer.
  5425.        Do not try risky measures here to get another type to use!
  5426.        Do not include stddef.h--it will fail!  */
  5427.     if ((HOST_WIDE_INT) cp & 3)
  5428.       cp += 4 - ((HOST_WIDE_INT) cp & 3);
  5429.     
  5430.     /* Now get the string. */
  5431.     str = (STRINGDEF *) (GENERIC_PTR) cp;
  5432.     string_start = cp += sizeof (STRINGDEF);
  5433.     
  5434.     for (; *cp; cp++)        /* skip the string */
  5435.       ;
  5436.     
  5437.     /* We need to macro expand the string here to ensure that the
  5438.        proper definition environment is in place.  If it were only
  5439.        expanded when we find out it is needed, macros necessary for
  5440.        its proper expansion might have had their definitions changed. */
  5441.     tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
  5442.     /* Lineno is already set in the precompiled file */
  5443.     str->contents = tmpbuf.buf;
  5444.     str->len = tmpbuf.length;
  5445.     str->writeflag = 0;
  5446.     str->filename = name;
  5447.     str->output_mark = outbuf.bufp - outbuf.buf;
  5448.     
  5449.     str->chain = 0;
  5450.     *stringlist_tailp = str;
  5451.     stringlist_tailp = &str->chain;
  5452.     
  5453.     /* Next comes a fourbyte number indicating the number of keys */
  5454.     /* for this string. */
  5455.     nkeys = *cp++;
  5456.     nkeys = (nkeys << 8) | *cp++;
  5457.     nkeys = (nkeys << 8) | *cp++;
  5458.     nkeys = (nkeys << 8) | *cp++;
  5459.  
  5460.     /* If this number is -1, then the string is mandatory. */
  5461.     if (nkeys == -1)
  5462.       str->writeflag = 1;
  5463.     else
  5464.       /* Otherwise, for each key, */
  5465.       for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
  5466.     KEYDEF *kp = (KEYDEF *) (GENERIC_PTR) cp;
  5467.     HASHNODE *hp;
  5468.     
  5469.     /* It starts with a KEYDEF structure */
  5470.     cp += sizeof (KEYDEF);
  5471.     
  5472.     /* Find the end of the key.  At the end of this for loop we
  5473.        advance CP to the start of the next key using this variable. */
  5474.     endofthiskey = cp + strlen ((char *) cp);
  5475.     kp->str = str;
  5476.     
  5477.     /* Expand the key, and enter it into the hash table. */
  5478.     tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
  5479.     tmpbuf.bufp = tmpbuf.buf;
  5480.     
  5481.     while (is_hor_space[*tmpbuf.bufp])
  5482.       tmpbuf.bufp++;
  5483.     if (!is_idstart[*tmpbuf.bufp]
  5484.         || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
  5485.       str->writeflag = 1;
  5486.       continue;
  5487.     }
  5488.         
  5489.     hp = lookup (tmpbuf.bufp, -1, -1);
  5490.     if (hp == NULL) {
  5491.       kp->chain = 0;
  5492.       install (tmpbuf.bufp, -1, T_PCSTRING, (char *) kp, -1);
  5493.     }
  5494.     else if (hp->type == T_PCSTRING) {
  5495.       kp->chain = hp->value.keydef;
  5496.       hp->value.keydef = kp;
  5497.     }
  5498.     else
  5499.       str->writeflag = 1;
  5500.       }
  5501.   }
  5502.   /* This output_line_directive serves to switch us back to the current
  5503.      input file in case some of these strings get output (which will 
  5504.      result in line directives for the header file being output). */
  5505.   output_line_directive (&instack[indepth], op, 0, enter_file);
  5506. }
  5507.  
  5508. /* Called from rescan when it hits a key for strings.  Mark them all */
  5509.  /* used and clean up. */
  5510. static void
  5511. pcstring_used (hp)
  5512.      HASHNODE *hp;
  5513. {
  5514.   KEYDEF *kp;
  5515.   
  5516.   for (kp = hp->value.keydef; kp; kp = kp->chain)
  5517.     kp->str->writeflag = 1;
  5518.   delete_macro (hp);
  5519. }
  5520.  
  5521. /* Write the output, interspersing precompiled strings in their */
  5522.  /* appropriate places. */
  5523. static void
  5524. write_output ()
  5525. {
  5526.   STRINGDEF *next_string;
  5527.   U_CHAR *cur_buf_loc;
  5528.   int line_directive_len = 80;
  5529.   char *line_directive = xmalloc (line_directive_len);
  5530.   int len;
  5531.  
  5532.   /* In each run through the loop, either cur_buf_loc == */
  5533.   /* next_string_loc, in which case we print a series of strings, or */
  5534.   /* it is less than next_string_loc, in which case we write some of */
  5535.   /* the buffer. */
  5536.   cur_buf_loc = outbuf.buf; 
  5537.   next_string = stringlist;
  5538.   
  5539.   while (cur_buf_loc < outbuf.bufp || next_string) {
  5540.     if (next_string
  5541.     && cur_buf_loc - outbuf.buf == next_string->output_mark) {
  5542.       if (next_string->writeflag) {
  5543.     len = 4 * strlen ((char *) next_string->filename) + 32;
  5544.     while (len > line_directive_len)
  5545.       line_directive = xrealloc (line_directive, 
  5546.                      line_directive_len *= 2);
  5547.     sprintf (line_directive, "\n# %d ", next_string->lineno);
  5548.     strcpy (quote_string (line_directive + strlen (line_directive),
  5549.                   (char *) next_string->filename),
  5550.         "\n");
  5551.     safe_write (fileno (stdout), line_directive, strlen (line_directive));
  5552.     safe_write (fileno (stdout),
  5553.             (char *) next_string->contents, next_string->len);
  5554.       }          
  5555.       next_string = next_string->chain;
  5556.     }
  5557.     else {
  5558.       len = (next_string
  5559.          ? (next_string->output_mark 
  5560.         - (cur_buf_loc - outbuf.buf))
  5561.          : outbuf.bufp - cur_buf_loc);
  5562.       
  5563.       safe_write (fileno (stdout), (char *) cur_buf_loc, len);
  5564.       cur_buf_loc += len;
  5565.     }
  5566.   }
  5567.   free (line_directive);
  5568. }
  5569.  
  5570. /* Pass a directive through to the output file.
  5571.    BUF points to the contents of the directive, as a contiguous string.
  5572.    LIMIT points to the first character past the end of the directive.
  5573.    KEYWORD is the keyword-table entry for the directive.  */
  5574.  
  5575. static void
  5576. pass_thru_directive (buf, limit, op, keyword)
  5577.      U_CHAR *buf, *limit;
  5578.      FILE_BUF *op;
  5579.      struct directive *keyword;
  5580. {
  5581.   register unsigned keyword_length = keyword->length;
  5582.  
  5583.   check_expand (op, 1 + keyword_length + (limit - buf));
  5584.   *op->bufp++ = '#';
  5585.   bcopy (keyword->name, (char *) op->bufp, keyword_length);
  5586.   op->bufp += keyword_length;
  5587.   if (limit != buf && buf[0] != ' ')
  5588.     *op->bufp++ = ' ';
  5589.   bcopy ((char *) buf, (char *) op->bufp, limit - buf);
  5590.   op->bufp += (limit - buf);
  5591. #if 0
  5592.   *op->bufp++ = '\n';
  5593.   /* Count the line we have just made in the output,
  5594.      to get in sync properly.  */
  5595.   op->lineno++;
  5596. #endif
  5597. }
  5598.  
  5599. /* The arglist structure is built by do_define to tell
  5600.    collect_definition where the argument names begin.  That
  5601.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  5602.    would contain pointers to the strings x, y, and z.
  5603.    Collect_definition would then build a DEFINITION node,
  5604.    with reflist nodes pointing to the places x, y, and z had
  5605.    appeared.  So the arglist is just convenience data passed
  5606.    between these two routines.  It is not kept around after
  5607.    the current #define has been processed and entered into the
  5608.    hash table. */
  5609.  
  5610. struct arglist {
  5611.   struct arglist *next;
  5612.   U_CHAR *name;
  5613.   int length;
  5614.   int argno;
  5615.   char rest_args;
  5616. };
  5617.  
  5618. /* Create a DEFINITION node from a #define directive.  Arguments are 
  5619.    as for do_define. */
  5620. static MACRODEF
  5621. create_definition (buf, limit, op)
  5622.      U_CHAR *buf, *limit;
  5623.      FILE_BUF *op;
  5624. {
  5625.   U_CHAR *bp;            /* temp ptr into input buffer */
  5626.   U_CHAR *symname;        /* remember where symbol name starts */
  5627.   int sym_length;        /* and how long it is */
  5628.   int line = instack[indepth].lineno;
  5629.   char *file = instack[indepth].nominal_fname;
  5630.   int rest_args = 0;
  5631.  
  5632.   DEFINITION *defn;
  5633.   int arglengths = 0;        /* Accumulate lengths of arg names
  5634.                    plus number of args.  */
  5635.   MACRODEF mdef;
  5636.  
  5637.   bp = buf;
  5638.  
  5639.   while (is_hor_space[*bp])
  5640.     bp++;
  5641.  
  5642.   symname = bp;            /* remember where it starts */
  5643.   sym_length = check_macro_name (bp, "macro");
  5644.   bp += sym_length;
  5645.  
  5646.   /* Lossage will occur if identifiers or control keywords are broken
  5647.      across lines using backslash.  This is not the right place to take
  5648.      care of that. */
  5649.  
  5650.   if (*bp == '(') {
  5651.     struct arglist *arg_ptrs = NULL;
  5652.     int argno = 0;
  5653.  
  5654.     bp++;            /* skip '(' */
  5655.     SKIP_WHITE_SPACE (bp);
  5656.  
  5657.     /* Loop over macro argument names.  */
  5658.     while (*bp != ')') {
  5659.       struct arglist *temp;
  5660.  
  5661.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  5662.       temp->name = bp;
  5663.       temp->next = arg_ptrs;
  5664.       temp->argno = argno++;
  5665.       temp->rest_args = 0;
  5666.       arg_ptrs = temp;
  5667.  
  5668.       if (rest_args)
  5669.     pedwarn ("another parameter follows `%s'",
  5670.          rest_extension);
  5671.  
  5672.       if (!is_idstart[*bp])
  5673.     pedwarn ("invalid character in macro parameter name");
  5674.       
  5675.       /* Find the end of the arg name.  */
  5676.       while (is_idchar[*bp]) {
  5677.     bp++;
  5678.     /* do we have a "special" rest-args extension here? */
  5679.     if (limit - bp > REST_EXTENSION_LENGTH &&
  5680.         bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
  5681.       rest_args = 1;
  5682.       temp->rest_args = 1;
  5683.       break;
  5684.     }
  5685.       }
  5686.       temp->length = bp - temp->name;
  5687.       if (rest_args == 1)
  5688.     bp += REST_EXTENSION_LENGTH;
  5689.       arglengths += temp->length + 2;
  5690.       SKIP_WHITE_SPACE (bp);
  5691.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  5692.     error ("badly punctuated parameter list in `#define'");
  5693.     goto nope;
  5694.       }
  5695.       if (*bp == ',') {
  5696.     bp++;
  5697.     SKIP_WHITE_SPACE (bp);
  5698.     /* A comma at this point can only be followed by an identifier.  */
  5699.     if (!is_idstart[*bp]) {
  5700.       error ("badly punctuated parameter list in `#define'");
  5701.       goto nope;
  5702.     }
  5703.       }
  5704.       if (bp >= limit) {
  5705.     error ("unterminated parameter list in `#define'");
  5706.     goto nope;
  5707.       }
  5708.       {
  5709.     struct arglist *otemp;
  5710.  
  5711.     for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
  5712.       if (temp->length == otemp->length &&
  5713.           bcmp (temp->name, otemp->name, temp->length) == 0) {
  5714.           error ("duplicate argument name `%.*s' in `#define'",
  5715.              temp->length, temp->name);
  5716.           goto nope;
  5717.       }
  5718.       }
  5719.     }
  5720.  
  5721.     ++bp;            /* skip paren */
  5722.     SKIP_WHITE_SPACE (bp);
  5723.     /* now everything from bp before limit is the definition. */
  5724.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  5725.     defn->rest_args = rest_args;
  5726.  
  5727.     /* Now set defn->args.argnames to the result of concatenating
  5728.        the argument names in reverse order
  5729.        with comma-space between them.  */
  5730.     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
  5731.     {
  5732.       struct arglist *temp;
  5733.       int i = 0;
  5734.       for (temp = arg_ptrs; temp; temp = temp->next) {
  5735.     bcopy (temp->name, &defn->args.argnames[i], temp->length);
  5736.     i += temp->length;
  5737.     if (temp->next != 0) {
  5738.       defn->args.argnames[i++] = ',';
  5739.       defn->args.argnames[i++] = ' ';
  5740.     }
  5741.       }
  5742.       defn->args.argnames[i] = 0;
  5743.     }
  5744.   } else {
  5745.     /* Simple expansion or empty definition.  */
  5746.  
  5747.     if (bp < limit)
  5748.       {
  5749.     if (is_hor_space[*bp]) {
  5750.       bp++;
  5751.       SKIP_WHITE_SPACE (bp);
  5752.     } else {
  5753.       switch (*bp) {
  5754.         case '!':  case '"':  case '#':  case '%':  case '&':  case '\'':
  5755.         case ')':  case '*':  case '+':  case ',':  case '-':  case '.':
  5756.         case '/':  case ':':  case ';':  case '<':  case '=':  case '>':
  5757.         case '?':  case '[':  case '\\': case ']':  case '^':  case '{':
  5758.         case '|':  case '}':  case '~':
  5759.           warning ("missing white space after `#define %.*s'",
  5760.                sym_length, symname);
  5761.           break;
  5762.  
  5763.         default:
  5764.           pedwarn ("missing white space after `#define %.*s'",
  5765.                sym_length, symname);
  5766.           break;
  5767.       }
  5768.     }
  5769.       }
  5770.     /* Now everything from bp before limit is the definition. */
  5771.     defn = collect_expansion (bp, limit, -1, NULL_PTR);
  5772.     defn->args.argnames = (U_CHAR *) "";
  5773.   }
  5774.  
  5775.   defn->line = line;
  5776.   defn->file = file;
  5777.  
  5778.   /* OP is null if this is a predefinition */
  5779.   defn->predefined = !op;
  5780.   mdef.defn = defn;
  5781.   mdef.symnam = symname;
  5782.   mdef.symlen = sym_length;
  5783.  
  5784.   return mdef;
  5785.  
  5786.  nope:
  5787.   mdef.defn = 0;
  5788.   return mdef;
  5789. }
  5790.  
  5791. /* Process a #define directive.
  5792. BUF points to the contents of the #define directive, as a contiguous string.
  5793. LIMIT points to the first character past the end of the definition.
  5794. KEYWORD is the keyword-table entry for #define.  */
  5795.  
  5796. static int
  5797. do_define (buf, limit, op, keyword)
  5798.      U_CHAR *buf, *limit;
  5799.      FILE_BUF *op;
  5800.      struct directive *keyword;
  5801. {
  5802.   int hashcode;
  5803.   MACRODEF mdef;
  5804.  
  5805.   /* If this is a precompiler run (with -pcp) pass thru #define directives.  */
  5806.   if (pcp_outfile && op)
  5807.     pass_thru_directive (buf, limit, op, keyword);
  5808.  
  5809.   mdef = create_definition (buf, limit, op);
  5810.   if (mdef.defn == 0)
  5811.     goto nope;
  5812.  
  5813.   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
  5814.  
  5815.   {
  5816.     HASHNODE *hp;
  5817.     if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
  5818.       int ok = 0;
  5819.       /* Redefining a precompiled key is ok.  */
  5820.       if (hp->type == T_PCSTRING)
  5821.     ok = 1;
  5822.       /* Redefining a macro is ok if the definitions are the same.  */
  5823.       else if (hp->type == T_MACRO)
  5824.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  5825.       /* Redefining a constant is ok with -D.  */
  5826.       else if (hp->type == T_CONST)
  5827.         ok = ! done_initializing;
  5828.       /* Print the warning if it's not ok.  */
  5829.       if (!ok) {
  5830.         /* If we are passing through #define and #undef directives, do
  5831.        that for this re-definition now.  */
  5832.         if (debug_output && op)
  5833.       pass_thru_directive (buf, limit, op, keyword);
  5834.  
  5835.     pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
  5836.     if (hp->type == T_MACRO)
  5837.       pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
  5838.                       "this is the location of the previous definition");
  5839.       }
  5840.       /* Replace the old definition.  */
  5841.       hp->type = T_MACRO;
  5842.       hp->value.defn = mdef.defn;
  5843.     } else {
  5844.       /* If we are passing through #define and #undef directives, do
  5845.      that for this new definition now.  */
  5846.       if (debug_output && op)
  5847.     pass_thru_directive (buf, limit, op, keyword);
  5848.       install (mdef.symnam, mdef.symlen, T_MACRO,
  5849.            (char *) mdef.defn, hashcode);
  5850.     }
  5851.   }
  5852.  
  5853.   return 0;
  5854.  
  5855. nope:
  5856.  
  5857.   return 1;
  5858. }
  5859.  
  5860. /* Check a purported macro name SYMNAME, and yield its length.
  5861.    USAGE is the kind of name this is intended for.  */
  5862.  
  5863. static int
  5864. check_macro_name (symname, usage)
  5865.      U_CHAR *symname;
  5866.      char *usage;
  5867. {
  5868.   U_CHAR *p;
  5869.   int sym_length;
  5870.  
  5871.   for (p = symname; is_idchar[*p]; p++)
  5872.     ;
  5873.   sym_length = p - symname;
  5874.   if (sym_length == 0)
  5875.     error ("invalid %s name", usage);
  5876.   else if (!is_idstart[*symname]
  5877.        || (sym_length == 7 && ! bcmp (symname, "defined", 7)))
  5878.     error ("invalid %s name `%.*s'", usage, sym_length, symname);
  5879.   return sym_length;
  5880. }
  5881.  
  5882. /*
  5883.  * return zero if two DEFINITIONs are isomorphic
  5884.  */
  5885. static int
  5886. compare_defs (d1, d2)
  5887.      DEFINITION *d1, *d2;
  5888. {
  5889.   register struct reflist *a1, *a2;
  5890.   register U_CHAR *p1 = d1->expansion;
  5891.   register U_CHAR *p2 = d2->expansion;
  5892.   int first = 1;
  5893.  
  5894.   if (d1->nargs != d2->nargs)
  5895.     return 1;
  5896.   if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
  5897.     return 1;
  5898.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  5899.        a1 = a1->next, a2 = a2->next) {
  5900.     if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars))
  5901.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  5902.     || a1->argno != a2->argno
  5903.     || a1->stringify != a2->stringify
  5904.     || a1->raw_before != a2->raw_before
  5905.     || a1->raw_after != a2->raw_after)
  5906.       return 1;
  5907.     first = 0;
  5908.     p1 += a1->nchars;
  5909.     p2 += a2->nchars;
  5910.   }
  5911.   if (a1 != a2)
  5912.     return 1;
  5913.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  5914.              p2, d2->length - (p2 - d2->expansion), 1))
  5915.     return 1;
  5916.   return 0;
  5917. }
  5918.  
  5919. /* Return 1 if two parts of two macro definitions are effectively different.
  5920.    One of the parts starts at BEG1 and has LEN1 chars;
  5921.    the other has LEN2 chars at BEG2.
  5922.    Any sequence of whitespace matches any other sequence of whitespace.
  5923.    FIRST means these parts are the first of a macro definition;
  5924.     so ignore leading whitespace entirely.
  5925.    LAST means these parts are the last of a macro definition;
  5926.     so ignore trailing whitespace entirely.  */
  5927.  
  5928. static int
  5929. comp_def_part (first, beg1, len1, beg2, len2, last)
  5930.      int first;
  5931.      U_CHAR *beg1, *beg2;
  5932.      int len1, len2;
  5933.      int last;
  5934. {
  5935.   register U_CHAR *end1 = beg1 + len1;
  5936.   register U_CHAR *end2 = beg2 + len2;
  5937.   if (first) {
  5938.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  5939.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  5940.   }
  5941.   if (last) {
  5942.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  5943.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  5944.   }
  5945.   while (beg1 != end1 && beg2 != end2) {
  5946.     if (is_space[*beg1] && is_space[*beg2]) {
  5947.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  5948.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  5949.     } else if (*beg1 == *beg2) {
  5950.       beg1++; beg2++;
  5951.     } else break;
  5952.   }
  5953.   return (beg1 != end1) || (beg2 != end2);
  5954. }
  5955.  
  5956. /* Read a replacement list for a macro with parameters.
  5957.    Build the DEFINITION structure.
  5958.    Reads characters of text starting at BUF until END.
  5959.    ARGLIST specifies the formal parameters to look for
  5960.    in the text of the definition; NARGS is the number of args
  5961.    in that list, or -1 for a macro name that wants no argument list.
  5962.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  5963.    and NAMELEN is its length in characters.
  5964.    
  5965. Note that comments, backslash-newlines, and leading white space
  5966. have already been deleted from the argument.  */
  5967.  
  5968. /* If there is no trailing whitespace, a Newline Space is added at the end
  5969.    to prevent concatenation that would be contrary to the standard.  */
  5970.  
  5971. static DEFINITION *
  5972. collect_expansion (buf, end, nargs, arglist)
  5973.      U_CHAR *buf, *end;
  5974.      int nargs;
  5975.      struct arglist *arglist;
  5976. {
  5977.   DEFINITION *defn;
  5978.   register U_CHAR *p, *limit, *lastp, *exp_p;
  5979.   struct reflist *endpat = NULL;
  5980.   /* Pointer to first nonspace after last ## seen.  */
  5981.   U_CHAR *concat = 0;
  5982.   /* Pointer to first nonspace after last single-# seen.  */
  5983.   U_CHAR *stringify = 0;
  5984.   /* How those tokens were spelled.  */
  5985.   enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN;
  5986.   enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN;
  5987.   int maxsize;
  5988.   int expected_delimiter = '\0';
  5989.  
  5990.   /* Scan thru the replacement list, ignoring comments and quoted
  5991.      strings, picking up on the macro calls.  It does a linear search
  5992.      thru the arg list on every potential symbol.  Profiling might say
  5993.      that something smarter should happen. */
  5994.  
  5995.   if (end < buf)
  5996.     abort ();
  5997.  
  5998.   /* Find the beginning of the trailing whitespace.  */
  5999.   limit = end;
  6000.   p = buf;
  6001.   while (p < limit && is_space[limit[-1]]) limit--;
  6002.  
  6003.   /* Allocate space for the text in the macro definition.
  6004.      Each input char may or may not need 1 byte,
  6005.      so this is an upper bound.
  6006.      The extra 3 are for invented trailing newline-marker and final null.  */
  6007.   maxsize = (sizeof (DEFINITION)
  6008.          + (limit - p) + 3);
  6009.   defn = (DEFINITION *) xcalloc (1, maxsize);
  6010.  
  6011.   defn->nargs = nargs;
  6012.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  6013.   lastp = exp_p;
  6014.  
  6015.   if (p[0] == '#'
  6016.       ? p[1] == '#'
  6017.       : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') {
  6018.     error ("`##' at start of macro definition");
  6019.     p += p[0] == '#' ? 2 : 4;
  6020.   }
  6021.  
  6022.   /* Process the main body of the definition.  */
  6023.   while (p < limit) {
  6024.     int skipped_arg = 0;
  6025.     register U_CHAR c = *p++;
  6026.  
  6027.     *exp_p++ = c;
  6028.  
  6029.     if (!traditional) {
  6030.       switch (c) {
  6031.       case '\'':
  6032.       case '\"':
  6033.         if (expected_delimiter != '\0') {
  6034.           if (c == expected_delimiter)
  6035.             expected_delimiter = '\0';
  6036.         } else
  6037.           expected_delimiter = c;
  6038.     break;
  6039.  
  6040.       case '\\':
  6041.     if (p < limit && expected_delimiter) {
  6042.       /* In a string, backslash goes through
  6043.          and makes next char ordinary.  */
  6044.       *exp_p++ = *p++;
  6045.     }
  6046.     break;
  6047.  
  6048.       case '%':
  6049.     if (!expected_delimiter && *p == ':') {
  6050.       /* %: is not a digraph if preceded by an odd number of '<'s.  */
  6051.       U_CHAR *p0 = p - 1;
  6052.       while (buf < p0 && p0[-1] == '<')
  6053.         p0--;
  6054.       if ((p - p0) & 1) {
  6055.         /* Treat %:%: as ## and %: as #.  */
  6056.         if (p[1] == '%' && p[2] == ':') {
  6057.           p += 2;
  6058.           goto sharp_sharp_token;
  6059.         }
  6060.         if (nargs >= 0) {
  6061.           p++;
  6062.           goto sharp_token;
  6063.         }
  6064.       }
  6065.     }
  6066.     break;
  6067.  
  6068.       case '#':
  6069.     /* # is ordinary inside a string.  */
  6070.     if (expected_delimiter)
  6071.       break;
  6072.     if (*p == '#') {
  6073.     sharp_sharp_token:
  6074.       /* ##: concatenate preceding and following tokens.  */
  6075.       /* Take out the first #, discard preceding whitespace.  */
  6076.       exp_p--;
  6077.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  6078.         --exp_p;
  6079.       /* Skip the second #.  */
  6080.       p++;
  6081.       concat_sharp_token_type = c;
  6082.       if (is_hor_space[*p]) {
  6083.         concat_sharp_token_type = c + 1;
  6084.         p++;
  6085.         SKIP_WHITE_SPACE (p);
  6086.       }
  6087.       concat = p;
  6088.       if (p == limit)
  6089.         error ("`##' at end of macro definition");
  6090.     } else if (nargs >= 0) {
  6091.       /* Single #: stringify following argument ref.
  6092.          Don't leave the # in the expansion.  */
  6093.     sharp_token:
  6094.       exp_p--;
  6095.       stringify_sharp_token_type = c;
  6096.       if (is_hor_space[*p]) {
  6097.         stringify_sharp_token_type = c + 1;
  6098.         p++;
  6099.         SKIP_WHITE_SPACE (p);
  6100.       }
  6101.       if (! is_idstart[*p] || nargs == 0)
  6102.         error ("`#' operator is not followed by a macro argument name");
  6103.       else
  6104.         stringify = p;
  6105.     }
  6106.     break;
  6107.       }
  6108.     } else {
  6109.       /* In -traditional mode, recognize arguments inside strings and
  6110.      and character constants, and ignore special properties of #.
  6111.      Arguments inside strings are considered "stringified", but no
  6112.      extra quote marks are supplied.  */
  6113.       switch (c) {
  6114.       case '\'':
  6115.       case '\"':
  6116.     if (expected_delimiter != '\0') {
  6117.       if (c == expected_delimiter)
  6118.         expected_delimiter = '\0';
  6119.     } else
  6120.       expected_delimiter = c;
  6121.     break;
  6122.  
  6123.       case '\\':
  6124.     /* Backslash quotes delimiters and itself, but not macro args.  */
  6125.     if (expected_delimiter != 0 && p < limit
  6126.         && (*p == expected_delimiter || *p == '\\')) {
  6127.       *exp_p++ = *p++;
  6128.       continue;
  6129.     }
  6130.     break;
  6131.  
  6132.       case '/':
  6133.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  6134.       break;
  6135.     if (*p == '*') {
  6136.       /* If we find a comment that wasn't removed by handle_directive,
  6137.          this must be -traditional.  So replace the comment with
  6138.          nothing at all.  */
  6139.       exp_p--;
  6140.       p += 1;
  6141.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  6142.         p++;
  6143. #if 0
  6144.       /* Mark this as a concatenation-point, as if it had been ##.  */
  6145.       concat = p;
  6146. #endif
  6147.     }
  6148.     break;
  6149.       }
  6150.     }
  6151.  
  6152.     /* Handle the start of a symbol.  */
  6153.     if (is_idchar[c] && nargs > 0) {
  6154.       U_CHAR *id_beg = p - 1;
  6155.       int id_len;
  6156.  
  6157.       --exp_p;
  6158.       while (p != limit && is_idchar[*p]) p++;
  6159.       id_len = p - id_beg;
  6160.  
  6161.       if (is_idstart[c]) {
  6162.     register struct arglist *arg;
  6163.  
  6164.     for (arg = arglist; arg != NULL; arg = arg->next) {
  6165.       struct reflist *tpat;
  6166.  
  6167.       if (arg->name[0] == c
  6168.           && arg->length == id_len
  6169.           && bcmp (arg->name, id_beg, id_len) == 0) {
  6170.         enum sharp_token_type tpat_stringify;
  6171.         if (expected_delimiter) {
  6172.           if (warn_stringify) {
  6173.         if (traditional) {
  6174.           warning ("macro argument `%.*s' is stringified.",
  6175.                id_len, arg->name);
  6176.         } else {
  6177.           warning ("macro arg `%.*s' would be stringified with -traditional.",
  6178.                id_len, arg->name);
  6179.         }
  6180.           }
  6181.           /* If ANSI, don't actually substitute inside a string.  */
  6182.           if (!traditional)
  6183.         break;
  6184.           tpat_stringify = SHARP_TOKEN;
  6185.         } else {
  6186.           tpat_stringify
  6187.         = (stringify == id_beg
  6188.            ? stringify_sharp_token_type : NO_SHARP_TOKEN);
  6189.         }
  6190.         /* make a pat node for this arg and append it to the end of
  6191.            the pat list */
  6192.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  6193.         tpat->next = NULL;
  6194.         tpat->raw_before
  6195.           = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN;
  6196.         tpat->raw_after = NO_SHARP_TOKEN;
  6197.         tpat->rest_args = arg->rest_args;
  6198.         tpat->stringify = tpat_stringify;
  6199.  
  6200.         if (endpat == NULL)
  6201.           defn->pattern = tpat;
  6202.         else
  6203.           endpat->next = tpat;
  6204.         endpat = tpat;
  6205.  
  6206.         tpat->argno = arg->argno;
  6207.         tpat->nchars = exp_p - lastp;
  6208.         {
  6209.           register U_CHAR *p1 = p;
  6210.           SKIP_WHITE_SPACE (p1);
  6211.           if (p1[0]=='#'
  6212.               ? p1[1]=='#'
  6213.           : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':')
  6214.         tpat->raw_after = p1[0] + (p != p1);
  6215.         }
  6216.         lastp = exp_p;    /* place to start copying from next time */
  6217.         skipped_arg = 1;
  6218.         break;
  6219.       }
  6220.     }
  6221.       }
  6222.  
  6223.       /* If this was not a macro arg, copy it into the expansion.  */
  6224.       if (! skipped_arg) {
  6225.     register U_CHAR *lim1 = p;
  6226.     p = id_beg;
  6227.     while (p != lim1)
  6228.       *exp_p++ = *p++;
  6229.     if (stringify == id_beg)
  6230.       error ("`#' operator should be followed by a macro argument name");
  6231.       }
  6232.     }
  6233.   }
  6234.  
  6235.   if (!traditional && expected_delimiter == 0) {
  6236.     /* If ANSI, put in a newline-space marker to prevent token pasting.
  6237.        But not if "inside a string" (which in ANSI mode happens only for
  6238.        -D option).  */
  6239.     *exp_p++ = '\n';
  6240.     *exp_p++ = ' ';
  6241.   }
  6242.  
  6243.   *exp_p = '\0';
  6244.  
  6245.   defn->length = exp_p - defn->expansion;
  6246.  
  6247.   /* Crash now if we overrun the allocated size.  */
  6248.   if (defn->length + 1 > maxsize)
  6249.     abort ();
  6250.  
  6251. #if 0
  6252. /* This isn't worth the time it takes.  */
  6253.   /* give back excess storage */
  6254.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  6255. #endif
  6256.  
  6257.   return defn;
  6258. }
  6259.  
  6260. static int
  6261. do_assert (buf, limit, op, keyword)
  6262.      U_CHAR *buf, *limit;
  6263.      FILE_BUF *op;
  6264.      struct directive *keyword;
  6265. {
  6266.   U_CHAR *bp;            /* temp ptr into input buffer */
  6267.   U_CHAR *symname;        /* remember where symbol name starts */
  6268.   int sym_length;        /* and how long it is */
  6269.   struct arglist *tokens = NULL;
  6270.  
  6271.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  6272.     pedwarn ("ANSI C does not allow `#assert'");
  6273.  
  6274.   bp = buf;
  6275.  
  6276.   while (is_hor_space[*bp])
  6277.     bp++;
  6278.  
  6279.   symname = bp;            /* remember where it starts */
  6280.   sym_length = check_macro_name (bp, "assertion");
  6281.   bp += sym_length;
  6282.   /* #define doesn't do this, but we should.  */
  6283.   SKIP_WHITE_SPACE (bp);
  6284.  
  6285.   /* Lossage will occur if identifiers or control tokens are broken
  6286.      across lines using backslash.  This is not the right place to take
  6287.      care of that. */
  6288.  
  6289.   if (*bp != '(') {
  6290.     error ("missing token-sequence in `#assert'");
  6291.     return 1;
  6292.   }
  6293.  
  6294.   {
  6295.     int error_flag = 0;
  6296.  
  6297.     bp++;            /* skip '(' */
  6298.     SKIP_WHITE_SPACE (bp);
  6299.  
  6300.     tokens = read_token_list (&bp, limit, &error_flag);
  6301.     if (error_flag)
  6302.       return 1;
  6303.     if (tokens == 0) {
  6304.       error ("empty token-sequence in `#assert'");
  6305.       return 1;
  6306.     }
  6307.  
  6308.     ++bp;            /* skip paren */
  6309.     SKIP_WHITE_SPACE (bp);
  6310.   }
  6311.  
  6312.   /* If this name isn't already an assertion name, make it one.
  6313.      Error if it was already in use in some other way.  */
  6314.  
  6315.   {
  6316.     ASSERTION_HASHNODE *hp;
  6317.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  6318.     struct tokenlist_list *value
  6319.       = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
  6320.  
  6321.     hp = assertion_lookup (symname, sym_length, hashcode);
  6322.     if (hp == NULL) {
  6323.       if (sym_length == 7 && ! bcmp (symname, "defined", 7))
  6324.     error ("`defined' redefined as assertion");
  6325.       hp = assertion_install (symname, sym_length, hashcode);
  6326.     }
  6327.  
  6328.     /* Add the spec'd token-sequence to the list of such.  */
  6329.     value->tokens = tokens;
  6330.     value->next = hp->value;
  6331.     hp->value = value;
  6332.   }
  6333.  
  6334.   return 0;
  6335. }
  6336.  
  6337. static int
  6338. do_unassert (buf, limit, op, keyword)
  6339.      U_CHAR *buf, *limit;
  6340.      FILE_BUF *op;
  6341.      struct directive *keyword;
  6342. {
  6343.   U_CHAR *bp;            /* temp ptr into input buffer */
  6344.   U_CHAR *symname;        /* remember where symbol name starts */
  6345.   int sym_length;        /* and how long it is */
  6346.  
  6347.   struct arglist *tokens = NULL;
  6348.   int tokens_specified = 0;
  6349.  
  6350.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  6351.     pedwarn ("ANSI C does not allow `#unassert'");
  6352.  
  6353.   bp = buf;
  6354.  
  6355.   while (is_hor_space[*bp])
  6356.     bp++;
  6357.  
  6358.   symname = bp;            /* remember where it starts */
  6359.   sym_length = check_macro_name (bp, "assertion");
  6360.   bp += sym_length;
  6361.   /* #define doesn't do this, but we should.  */
  6362.   SKIP_WHITE_SPACE (bp);
  6363.  
  6364.   /* Lossage will occur if identifiers or control tokens are broken
  6365.      across lines using backslash.  This is not the right place to take
  6366.      care of that. */
  6367.  
  6368.   if (*bp == '(') {
  6369.     int error_flag = 0;
  6370.  
  6371.     bp++;            /* skip '(' */
  6372.     SKIP_WHITE_SPACE (bp);
  6373.  
  6374.     tokens = read_token_list (&bp, limit, &error_flag);
  6375.     if (error_flag)
  6376.       return 1;
  6377.     if (tokens == 0) {
  6378.       error ("empty token list in `#unassert'");
  6379.       return 1;
  6380.     }
  6381.  
  6382.     tokens_specified = 1;
  6383.  
  6384.     ++bp;            /* skip paren */
  6385.     SKIP_WHITE_SPACE (bp);
  6386.   }
  6387.  
  6388.   {
  6389.     ASSERTION_HASHNODE *hp;
  6390.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  6391.     struct tokenlist_list *tail, *prev;
  6392.  
  6393.     hp = assertion_lookup (symname, sym_length, hashcode);
  6394.     if (hp == NULL)
  6395.       return 1;
  6396.  
  6397.     /* If no token list was specified, then eliminate this assertion
  6398.        entirely.  */
  6399.     if (! tokens_specified) {
  6400.       struct tokenlist_list *next;
  6401.       for (tail = hp->value; tail; tail = next) {
  6402.     next = tail->next;
  6403.     free_token_list (tail->tokens);
  6404.     free (tail);
  6405.       }
  6406.       delete_assertion (hp);
  6407.     } else {
  6408.       /* If a list of tokens was given, then delete any matching list.  */
  6409.  
  6410.       tail = hp->value;
  6411.       prev = 0;
  6412.       while (tail) {
  6413.     struct tokenlist_list *next = tail->next;
  6414.     if (compare_token_lists (tail->tokens, tokens)) {
  6415.       if (prev)
  6416.         prev->next = next;
  6417.       else
  6418.         hp->value = tail->next;
  6419.       free_token_list (tail->tokens);
  6420.       free (tail);
  6421.     } else {
  6422.       prev = tail;
  6423.     }
  6424.     tail = next;
  6425.       }
  6426.     }
  6427.   }
  6428.  
  6429.   return 0;
  6430. }
  6431.  
  6432. /* Test whether there is an assertion named NAME
  6433.    and optionally whether it has an asserted token list TOKENS.
  6434.    NAME is not null terminated; its length is SYM_LENGTH.
  6435.    If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
  6436.  
  6437. int
  6438. check_assertion (name, sym_length, tokens_specified, tokens)
  6439.      U_CHAR *name;
  6440.      int sym_length;
  6441.      int tokens_specified;
  6442.      struct arglist *tokens;
  6443. {
  6444.   ASSERTION_HASHNODE *hp;
  6445.   int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
  6446.  
  6447.   if (pedantic && !instack[indepth].system_header_p)
  6448.     pedwarn ("ANSI C does not allow testing assertions");
  6449.  
  6450.   hp = assertion_lookup (name, sym_length, hashcode);
  6451.   if (hp == NULL)
  6452.     /* It is not an assertion; just return false.  */
  6453.     return 0;
  6454.  
  6455.   /* If no token list was specified, then value is 1.  */
  6456.   if (! tokens_specified)
  6457.     return 1;
  6458.  
  6459.   {
  6460.     struct tokenlist_list *tail;
  6461.  
  6462.     tail = hp->value;
  6463.  
  6464.     /* If a list of tokens was given,
  6465.        then succeed if the assertion records a matching list.  */
  6466.  
  6467.     while (tail) {
  6468.       if (compare_token_lists (tail->tokens, tokens))
  6469.     return 1;
  6470.       tail = tail->next;
  6471.     }
  6472.  
  6473.     /* Fail if the assertion has no matching list.  */
  6474.     return 0;
  6475.   }
  6476. }
  6477.  
  6478. /* Compare two lists of tokens for equality including order of tokens.  */
  6479.  
  6480. static int
  6481. compare_token_lists (l1, l2)
  6482.      struct arglist *l1, *l2;
  6483. {
  6484.   while (l1 && l2) {
  6485.     if (l1->length != l2->length)
  6486.       return 0;
  6487.     if (bcmp (l1->name, l2->name, l1->length))
  6488.       return 0;
  6489.     l1 = l1->next;
  6490.     l2 = l2->next;
  6491.   }
  6492.  
  6493.   /* Succeed if both lists end at the same time.  */
  6494.   return l1 == l2;
  6495. }
  6496.  
  6497. /* Read a space-separated list of tokens ending in a close parenthesis.
  6498.    Return a list of strings, in the order they were written.
  6499.    (In case of error, return 0 and store -1 in *ERROR_FLAG.)
  6500.    Parse the text starting at *BPP, and update *BPP.
  6501.    Don't parse beyond LIMIT.  */
  6502.  
  6503. static struct arglist *
  6504. read_token_list (bpp, limit, error_flag)
  6505.      U_CHAR **bpp;
  6506.      U_CHAR *limit;
  6507.      int *error_flag;
  6508. {
  6509.   struct arglist *token_ptrs = 0;
  6510.   U_CHAR *bp = *bpp;
  6511.   int depth = 1;
  6512.  
  6513.   *error_flag = 0;
  6514.  
  6515.   /* Loop over the assertion value tokens.  */
  6516.   while (depth > 0) {
  6517.     struct arglist *temp;
  6518.     int eofp = 0;
  6519.     U_CHAR *beg = bp;
  6520.  
  6521.     /* Find the end of the token.  */
  6522.     if (*bp == '(') {
  6523.       bp++;
  6524.       depth++;
  6525.     } else if (*bp == ')') {
  6526.       depth--;
  6527.       if (depth == 0)
  6528.     break;
  6529.       bp++;
  6530.     } else if (*bp == '"' || *bp == '\'')
  6531.       bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  6532.     else
  6533.       while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
  6534.          && *bp != '"' && *bp != '\'' && bp != limit)
  6535.     bp++;
  6536.  
  6537.     temp = (struct arglist *) xmalloc (sizeof (struct arglist));
  6538.     temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
  6539.     bcopy ((char *) beg, (char *) temp->name, bp - beg);
  6540.     temp->name[bp - beg] = 0;
  6541.     temp->next = token_ptrs;
  6542.     token_ptrs = temp;
  6543.     temp->length = bp - beg;
  6544.  
  6545.     SKIP_WHITE_SPACE (bp);
  6546.  
  6547.     if (bp >= limit) {
  6548.       error ("unterminated token sequence in `#assert' or `#unassert'");
  6549.       *error_flag = -1;
  6550.       return 0;
  6551.     }
  6552.   }
  6553.   *bpp = bp;
  6554.  
  6555.   /* We accumulated the names in reverse order.
  6556.      Now reverse them to get the proper order.  */
  6557.   {
  6558.     register struct arglist *prev = 0, *this, *next;
  6559.     for (this = token_ptrs; this; this = next) {
  6560.       next = this->next;
  6561.       this->next = prev;
  6562.       prev = this;
  6563.     }
  6564.     return prev;
  6565.   }
  6566. }
  6567.  
  6568. static void
  6569. free_token_list (tokens)
  6570.      struct arglist *tokens;
  6571. {
  6572.   while (tokens) {
  6573.     struct arglist *next = tokens->next;
  6574.     free (tokens->name);
  6575.     free (tokens);
  6576.     tokens = next;
  6577.   }
  6578. }
  6579.  
  6580. /*
  6581.  * Install a name in the assertion hash table.
  6582.  *
  6583.  * If LEN is >= 0, it is the length of the name.
  6584.  * Otherwise, compute the length by scanning the entire name.
  6585.  *
  6586.  * If HASH is >= 0, it is the precomputed hash code.
  6587.  * Otherwise, compute the hash code.
  6588.  */
  6589. static ASSERTION_HASHNODE *
  6590. assertion_install (name, len, hash)
  6591.      U_CHAR *name;
  6592.      int len;
  6593.      int hash;
  6594. {
  6595.   register ASSERTION_HASHNODE *hp;
  6596.   register int i, bucket;
  6597.   register U_CHAR *p, *q;
  6598.  
  6599.   i = sizeof (ASSERTION_HASHNODE) + len + 1;
  6600.   hp = (ASSERTION_HASHNODE *) xmalloc (i);
  6601.   bucket = hash;
  6602.   hp->bucket_hdr = &assertion_hashtab[bucket];
  6603.   hp->next = assertion_hashtab[bucket];
  6604.   assertion_hashtab[bucket] = hp;
  6605.   hp->prev = NULL;
  6606.   if (hp->next != NULL)
  6607.     hp->next->prev = hp;
  6608.   hp->length = len;
  6609.   hp->value = 0;
  6610.   hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
  6611.   p = hp->name;
  6612.   q = name;
  6613.   for (i = 0; i < len; i++)
  6614.     *p++ = *q++;
  6615.   hp->name[len] = 0;
  6616.   return hp;
  6617. }
  6618.  
  6619. /*
  6620.  * find the most recent hash node for name name (ending with first
  6621.  * non-identifier char) installed by install
  6622.  *
  6623.  * If LEN is >= 0, it is the length of the name.
  6624.  * Otherwise, compute the length by scanning the entire name.
  6625.  *
  6626.  * If HASH is >= 0, it is the precomputed hash code.
  6627.  * Otherwise, compute the hash code.
  6628.  */
  6629. static ASSERTION_HASHNODE *
  6630. assertion_lookup (name, len, hash)
  6631.      U_CHAR *name;
  6632.      int len;
  6633.      int hash;
  6634. {
  6635.   register ASSERTION_HASHNODE *bucket;
  6636.  
  6637.   bucket = assertion_hashtab[hash];
  6638.   while (bucket) {
  6639.     if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
  6640.       return bucket;
  6641.     bucket = bucket->next;
  6642.   }
  6643.   return NULL;
  6644. }
  6645.  
  6646. static void
  6647. delete_assertion (hp)
  6648.      ASSERTION_HASHNODE *hp;
  6649. {
  6650.  
  6651.   if (hp->prev != NULL)
  6652.     hp->prev->next = hp->next;
  6653.   if (hp->next != NULL)
  6654.     hp->next->prev = hp->prev;
  6655.  
  6656.   /* make sure that the bucket chain header that
  6657.      the deleted guy was on points to the right thing afterwards. */
  6658.   if (hp == *hp->bucket_hdr)
  6659.     *hp->bucket_hdr = hp->next;
  6660.  
  6661.   free (hp);
  6662. }
  6663.  
  6664. /*
  6665.  * interpret #line directive.  Remembers previously seen fnames
  6666.  * in its very own hash table.
  6667.  */
  6668. #define FNAME_HASHSIZE 37
  6669.  
  6670. static int
  6671. do_line (buf, limit, op, keyword)
  6672.      U_CHAR *buf, *limit;
  6673.      FILE_BUF *op;
  6674.      struct directive *keyword;
  6675. {
  6676.   register U_CHAR *bp;
  6677.   FILE_BUF *ip = &instack[indepth];
  6678.   FILE_BUF tem;
  6679.   int new_lineno;
  6680.   enum file_change_code file_change = same_file;
  6681.  
  6682.   /* Expand any macros.  */
  6683.   tem = expand_to_temp_buffer (buf, limit, 0, 0);
  6684.  
  6685.   /* Point to macroexpanded line, which is null-terminated now.  */
  6686.   bp = tem.buf;
  6687.   SKIP_WHITE_SPACE (bp);
  6688.  
  6689.   if (!isdigit (*bp)) {
  6690.     error ("invalid format `#line' directive");
  6691.     return 0;
  6692.   }
  6693.  
  6694.   /* The Newline at the end of this line remains to be processed.
  6695.      To put the next line at the specified line number,
  6696.      we must store a line number now that is one less.  */
  6697.   new_lineno = atoi ((char *) bp) - 1;
  6698.  
  6699.   /* NEW_LINENO is one less than the actual line number here.  */
  6700.   if (pedantic && new_lineno < 0)
  6701.     pedwarn ("line number out of range in `#line' directive");
  6702.  
  6703.   /* skip over the line number.  */
  6704.   while (isdigit (*bp))
  6705.     bp++;
  6706.  
  6707. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  6708.   if (*bp && !is_space[*bp]) {
  6709.     error ("invalid format `#line' directive");
  6710.     return;
  6711.   }
  6712. #endif
  6713.  
  6714.   SKIP_WHITE_SPACE (bp);
  6715.  
  6716.   if (*bp == '\"') {
  6717.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  6718.     HASHNODE *hp, **hash_bucket;
  6719.     U_CHAR *fname, *p;
  6720.     int fname_length;
  6721.  
  6722.     fname = ++bp;
  6723.  
  6724.     /* Turn the file name, which is a character string literal,
  6725.        into a null-terminated string.  Do this in place.  */
  6726.     p = bp;
  6727.     for (;;)
  6728.       switch ((*p++ = *bp++)) {
  6729.       case '\0':
  6730.     error ("invalid format `#line' directive");
  6731.     return 0;
  6732.  
  6733.       case '\\':
  6734.     {
  6735.       char *bpc = (char *) bp;
  6736.       int c = parse_escape (&bpc);
  6737.       bp = (U_CHAR *) bpc;
  6738.       if (c < 0)
  6739.         p--;
  6740.       else
  6741.         p[-1] = c;
  6742.     }
  6743.     break;
  6744.  
  6745.       case '\"':
  6746.     p[-1] = 0;
  6747.     goto fname_done;
  6748.       }
  6749.   fname_done:
  6750.     fname_length = p - fname;
  6751.  
  6752.     SKIP_WHITE_SPACE (bp);
  6753.     if (*bp) {
  6754.       if (pedantic)
  6755.     pedwarn ("garbage at end of `#line' directive");
  6756.       if (*bp == '1')
  6757.     file_change = enter_file;
  6758.       else if (*bp == '2')
  6759.     file_change = leave_file;
  6760.       else if (*bp == '3')
  6761.     ip->system_header_p = 1;
  6762.       else if (*bp == '4')
  6763.     ip->system_header_p = 2;
  6764.       else {
  6765.     error ("invalid format `#line' directive");
  6766.     return 0;
  6767.       }
  6768.  
  6769.       bp++;
  6770.       SKIP_WHITE_SPACE (bp);
  6771.       if (*bp == '3') {
  6772.     ip->system_header_p = 1;
  6773.     bp++;
  6774.     SKIP_WHITE_SPACE (bp);
  6775.       }
  6776.       if (*bp == '4') {
  6777.     ip->system_header_p = 2;
  6778.     bp++;
  6779.     SKIP_WHITE_SPACE (bp);
  6780.       }
  6781.       if (*bp) {
  6782.     error ("invalid format `#line' directive");
  6783.     return 0;
  6784.       }
  6785.     }
  6786.  
  6787.     hash_bucket =
  6788.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  6789.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  6790.       if (hp->length == fname_length &&
  6791.       bcmp (hp->value.cpval, fname, fname_length) == 0) {
  6792.     ip->nominal_fname = hp->value.cpval;
  6793.     break;
  6794.       }
  6795.     if (hp == 0) {
  6796.       /* Didn't find it; cons up a new one.  */
  6797.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  6798.       hp->next = *hash_bucket;
  6799.       *hash_bucket = hp;
  6800.  
  6801.       hp->length = fname_length;
  6802.       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  6803.       bcopy (fname, hp->value.cpval, fname_length);
  6804.     }
  6805.   } else if (*bp) {
  6806.     error ("invalid format `#line' directive");
  6807.     return 0;
  6808.   }
  6809.  
  6810.   ip->lineno = new_lineno;
  6811.   output_line_directive (ip, op, 0, file_change);
  6812.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  6813.   return 0;
  6814. }
  6815.  
  6816. /*
  6817.  * remove the definition of a symbol from the symbol table.
  6818.  * according to un*x /lib/cpp, it is not an error to undef
  6819.  * something that has no definitions, so it isn't one here either.
  6820.  */
  6821.  
  6822. static int
  6823. do_undef (buf, limit, op, keyword)
  6824.      U_CHAR *buf, *limit;
  6825.      FILE_BUF *op;
  6826.      struct directive *keyword;
  6827. {
  6828.   int sym_length;
  6829.   HASHNODE *hp;
  6830.   U_CHAR *orig_buf = buf;
  6831.  
  6832.   /* If this is a precompiler run (with -pcp) pass thru #undef directives.  */
  6833.   if (pcp_outfile && op)
  6834.     pass_thru_directive (buf, limit, op, keyword);
  6835.  
  6836.   SKIP_WHITE_SPACE (buf);
  6837.   sym_length = check_macro_name (buf, "macro");
  6838.  
  6839.   while ((hp = lookup (buf, sym_length, -1)) != NULL) {
  6840.     /* If we are generating additional info for debugging (with -g) we
  6841.        need to pass through all effective #undef directives.  */
  6842.     if (debug_output && op)
  6843.       pass_thru_directive (orig_buf, limit, op, keyword);
  6844.     if (hp->type != T_MACRO)
  6845.       warning ("undefining `%s'", hp->name);
  6846.     delete_macro (hp);
  6847.   }
  6848.  
  6849.   if (pedantic) {
  6850.     buf += sym_length;
  6851.     SKIP_WHITE_SPACE (buf);
  6852.     if (buf != limit)
  6853.       pedwarn ("garbage after `#undef' directive");
  6854.   }
  6855.   return 0;
  6856. }
  6857.  
  6858. /*
  6859.  * Report an error detected by the program we are processing.
  6860.  * Use the text of the line in the error message.
  6861.  * (We use error because it prints the filename & line#.)
  6862.  */
  6863.  
  6864. static int
  6865. do_error (buf, limit, op, keyword)
  6866.      U_CHAR *buf, *limit;
  6867.      FILE_BUF *op;
  6868.      struct directive *keyword;
  6869. {
  6870.   int length = limit - buf;
  6871.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  6872.   bcopy ((char *) buf, (char *) copy, length);
  6873.   copy[length] = 0;
  6874.   SKIP_WHITE_SPACE (copy);
  6875.   error ("#error %s", copy);
  6876.   return 0;
  6877. }
  6878.  
  6879. /*
  6880.  * Report a warning detected by the program we are processing.
  6881.  * Use the text of the line in the warning message, then continue.
  6882.  * (We use error because it prints the filename & line#.)
  6883.  */
  6884.  
  6885. static int
  6886. do_warning (buf, limit, op, keyword)
  6887.      U_CHAR *buf, *limit;
  6888.      FILE_BUF *op;
  6889.      struct directive *keyword;
  6890. {
  6891.   int length = limit - buf;
  6892.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  6893.   bcopy ((char *) buf, (char *) copy, length);
  6894.   copy[length] = 0;
  6895.   SKIP_WHITE_SPACE (copy);
  6896.   warning ("#warning %s", copy);
  6897.   return 0;
  6898. }
  6899.  
  6900. /* Remember the name of the current file being read from so that we can
  6901.    avoid ever including it again.  */
  6902.  
  6903. static void
  6904. do_once ()
  6905. {
  6906.   int i;
  6907.   FILE_BUF *ip = NULL;
  6908.  
  6909.   for (i = indepth; i >= 0; i--)
  6910.     if (instack[i].fname != NULL) {
  6911.       ip = &instack[i];
  6912.       break;
  6913.     }
  6914.  
  6915.   if (ip != NULL) {
  6916.     struct file_name_list *new;
  6917.     
  6918.     new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  6919.     new->next = dont_repeat_files;
  6920.     dont_repeat_files = new;
  6921.     new->fname = savestring (ip->fname);
  6922.     new->control_macro = 0;
  6923.     new->got_name_map = 0;
  6924.     new->c_system_include_path = 0;
  6925.   }
  6926. }
  6927.  
  6928. /* #ident has already been copied to the output file, so just ignore it.  */
  6929.  
  6930. static int
  6931. do_ident (buf, limit, op, keyword)
  6932.      U_CHAR *buf, *limit;
  6933.      FILE_BUF *op;
  6934.      struct directive *keyword;
  6935. {
  6936.   FILE_BUF trybuf;
  6937.   int len;
  6938.  
  6939.   /* Allow #ident in system headers, since that's not user's fault.  */
  6940.   if (pedantic && !instack[indepth].system_header_p)
  6941.     pedwarn ("ANSI C does not allow `#ident'");
  6942.  
  6943.   trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  6944.   buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  6945.   bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  6946.   limit = buf + (trybuf.bufp - trybuf.buf);
  6947.   len = (limit - buf);
  6948.   free (trybuf.buf);
  6949.  
  6950.   /* Output directive name.  */
  6951.   check_expand (op, 7);
  6952.   bcopy ("#ident ", (char *) op->bufp, 7);
  6953.   op->bufp += 7;
  6954.  
  6955.   /* Output the expanded argument line.  */
  6956.   check_expand (op, len);
  6957.   bcopy ((char *) buf, (char *) op->bufp, len);
  6958.   op->bufp += len;
  6959.  
  6960.   return 0;
  6961. }
  6962.  
  6963. /* #pragma and its argument line have already been copied to the output file.
  6964.    Just check for some recognized pragmas that need validation here.  */
  6965.  
  6966. static int
  6967. do_pragma (buf, limit, op, keyword)
  6968.      U_CHAR *buf, *limit;
  6969.      FILE_BUF *op;
  6970.      struct directive *keyword;
  6971. {
  6972.   SKIP_WHITE_SPACE (buf);
  6973.   if (!strncmp ((char *) buf, "once", 4)) {
  6974.     /* Allow #pragma once in system headers, since that's not the user's
  6975.        fault.  */
  6976.     if (!instack[indepth].system_header_p)
  6977.       warning ("`#pragma once' is obsolete");
  6978.     do_once ();
  6979.   }
  6980.  
  6981.   if (!strncmp ((char *) buf, "implementation", 14)) {
  6982.     /* Be quiet about `#pragma implementation' for a file only if it hasn't
  6983.        been included yet.  */
  6984.     struct file_name_list *ptr;
  6985.     U_CHAR *p = buf + 14, *fname, *inc_fname;
  6986.     SKIP_WHITE_SPACE (p);
  6987.     if (*p == '\n' || *p != '\"')
  6988.       return 0;
  6989.  
  6990.     fname = p + 1;
  6991.     if ((p = (U_CHAR *) index ((char *) fname, '\"')))
  6992.       *p = '\0';
  6993.     
  6994.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  6995.       inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
  6996.       inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
  6997.       if (inc_fname && !strcmp ((char *) inc_fname, (char *) fname))
  6998.     warning ("`#pragma implementation' for `%s' appears after file is included",
  6999.          fname);
  7000.     }
  7001.   }
  7002.  
  7003.   return 0;
  7004. }
  7005.  
  7006. #if 0
  7007. /* This was a fun hack, but #pragma seems to start to be useful.
  7008.    By failing to recognize it, we pass it through unchanged to cc1.  */
  7009.  
  7010. /*
  7011.  * the behavior of the #pragma directive is implementation defined.
  7012.  * this implementation defines it as follows.
  7013.  */
  7014.  
  7015. static int
  7016. do_pragma ()
  7017. {
  7018.   close (0);
  7019.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  7020.     goto nope;
  7021.   close (1);
  7022.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  7023.     goto nope;
  7024.   execl ("/usr/games/hack", "#pragma", 0);
  7025.   execl ("/usr/games/rogue", "#pragma", 0);
  7026.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  7027.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  7028. nope:
  7029.   fatal ("You are in a maze of twisty compiler features, all different");
  7030. }
  7031. #endif
  7032.  
  7033. #ifdef SCCS_DIRECTIVE
  7034.  
  7035. /* Just ignore #sccs, on systems where we define it at all.  */
  7036.  
  7037. static int
  7038. do_sccs (buf, limit, op, keyword)
  7039.      U_CHAR *buf, *limit;
  7040.      FILE_BUF *op;
  7041.      struct directive *keyword;
  7042. {
  7043.   if (pedantic)
  7044.     pedwarn ("ANSI C does not allow `#sccs'");
  7045.   return 0;
  7046. }
  7047.  
  7048. #endif /* defined (SCCS_DIRECTIVE) */
  7049.  
  7050. /*
  7051.  * handle #if directive by
  7052.  *   1) inserting special `defined' keyword into the hash table
  7053.  *    that gets turned into 0 or 1 by special_symbol (thus,
  7054.  *    if the luser has a symbol called `defined' already, it won't
  7055.  *      work inside the #if directive)
  7056.  *   2) rescan the input into a temporary output buffer
  7057.  *   3) pass the output buffer to the yacc parser and collect a value
  7058.  *   4) clean up the mess left from steps 1 and 2.
  7059.  *   5) call conditional_skip to skip til the next #endif (etc.),
  7060.  *      or not, depending on the value from step 3.
  7061.  */
  7062.  
  7063. static int
  7064. do_if (buf, limit, op, keyword)
  7065.      U_CHAR *buf, *limit;
  7066.      FILE_BUF *op;
  7067.      struct directive *keyword;
  7068. {
  7069.   HOST_WIDE_INT value;
  7070.   FILE_BUF *ip = &instack[indepth];
  7071.  
  7072.   value = eval_if_expression (buf, limit - buf);
  7073.   conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
  7074.   return 0;
  7075. }
  7076.  
  7077. /*
  7078.  * handle a #elif directive by not changing  if_stack  either.
  7079.  * see the comment above do_else.
  7080.  */
  7081.  
  7082. static int
  7083. do_elif (buf, limit, op, keyword)
  7084.      U_CHAR *buf, *limit;
  7085.      FILE_BUF *op;
  7086.      struct directive *keyword;
  7087. {
  7088.   HOST_WIDE_INT value;
  7089.   FILE_BUF *ip = &instack[indepth];
  7090.  
  7091.   if (if_stack == instack[indepth].if_stack) {
  7092.     error ("`#elif' not within a conditional");
  7093.     return 0;
  7094.   } else {
  7095.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  7096.       error ("`#elif' after `#else'");
  7097.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  7098.       if (if_stack->fname != NULL && ip->fname != NULL &&
  7099.       strcmp (if_stack->fname, ip->nominal_fname) != 0)
  7100.     fprintf (stderr, ", file %s", if_stack->fname);
  7101.       fprintf (stderr, ")\n");
  7102.     }
  7103.     if_stack->type = T_ELIF;
  7104.   }
  7105.  
  7106.   if (if_stack->if_succeeded)
  7107.     skip_if_group (ip, 0, op);
  7108.   else {
  7109.     value = eval_if_expression (buf, limit - buf);
  7110.     if (value == 0)
  7111.       skip_if_group (ip, 0, op);
  7112.     else {
  7113.       ++if_stack->if_succeeded;    /* continue processing input */
  7114.       output_line_directive (ip, op, 1, same_file);
  7115.     }
  7116.   }
  7117.   return 0;
  7118. }
  7119.  
  7120. /*
  7121.  * evaluate a #if expression in BUF, of length LENGTH,
  7122.  * then parse the result as a C expression and return the value as an int.
  7123.  */
  7124. static HOST_WIDE_INT
  7125. eval_if_expression (buf, length)
  7126.      U_CHAR *buf;
  7127.      int length;
  7128. {
  7129.   FILE_BUF temp_obuf;
  7130.   HASHNODE *save_defined;
  7131.   HOST_WIDE_INT value;
  7132.  
  7133.   save_defined = install ((U_CHAR *) "defined", -1, T_SPEC_DEFINED,
  7134.               NULL_PTR, -1);
  7135.   pcp_inside_if = 1;
  7136.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
  7137.   pcp_inside_if = 0;
  7138.   delete_macro (save_defined);    /* clean up special symbol */
  7139.  
  7140.   value = parse_c_expression ((char *) temp_obuf.buf);
  7141.  
  7142.   free (temp_obuf.buf);
  7143.  
  7144.   return value;
  7145. }
  7146.  
  7147. /*
  7148.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  7149.  * then do or don't skip to the #endif/#else/#elif depending
  7150.  * on what directive is actually being processed.
  7151.  */
  7152.  
  7153. static int
  7154. do_xifdef (buf, limit, op, keyword)
  7155.      U_CHAR *buf, *limit;
  7156.      FILE_BUF *op;
  7157.      struct directive *keyword;
  7158. {
  7159.   int skip;
  7160.   FILE_BUF *ip = &instack[indepth];
  7161.   U_CHAR *end; 
  7162.   int start_of_file = 0;
  7163.   U_CHAR *control_macro = 0;
  7164.  
  7165.   /* Detect a #ifndef at start of file (not counting comments).  */
  7166.   if (ip->fname != 0 && keyword->type == T_IFNDEF) {
  7167.     U_CHAR *p = ip->buf;
  7168.     while (p != directive_start) {
  7169.       U_CHAR c = *p++;
  7170.       if (is_space[c])
  7171.     ;
  7172.       /* Make no special provision for backslash-newline here; this is
  7173.      slower if backslash-newlines are present, but it's correct,
  7174.      and it's not worth it to tune for the rare backslash-newline.  */
  7175.       else if (c == '/'
  7176.            && (*p == '*' || (cplusplus_comments && *p == '/'))) {
  7177.     /* Skip this comment.  */
  7178.     int junk = 0;
  7179.     U_CHAR *save_bufp = ip->bufp;
  7180.     ip->bufp = p + 1;
  7181.     p = skip_to_end_of_comment (ip, &junk, 1);
  7182.     ip->bufp = save_bufp;
  7183.       } else {
  7184.     goto fail;
  7185.       }
  7186.     }
  7187.     /* If we get here, this conditional is the beginning of the file.  */
  7188.     start_of_file = 1;
  7189.   fail: ;
  7190.   }
  7191.  
  7192.   /* Discard leading and trailing whitespace.  */
  7193.   SKIP_WHITE_SPACE (buf);
  7194.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  7195.  
  7196.   /* Find the end of the identifier at the beginning.  */
  7197.   for (end = buf; is_idchar[*end]; end++);
  7198.  
  7199.   if (end == buf) {
  7200.     skip = (keyword->type == T_IFDEF);
  7201.     if (! traditional)
  7202.       pedwarn (end == limit ? "`#%s' with no argument"
  7203.            : "`#%s' argument starts with punctuation",
  7204.            keyword->name);
  7205.   } else {
  7206.     HASHNODE *hp;
  7207.  
  7208.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  7209.       pedwarn ("`#%s' argument starts with a digit", keyword->name);
  7210.     else if (end != limit && !traditional)
  7211.       pedwarn ("garbage at end of `#%s' argument", keyword->name);
  7212.  
  7213.     hp = lookup (buf, end-buf, -1);
  7214.  
  7215.     if (pcp_outfile) {
  7216.       /* Output a precondition for this macro.  */
  7217.       if (hp &&
  7218.       (hp->type == T_CONST
  7219.        || (hp->type == T_MACRO && hp->value.defn->predefined)))
  7220.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  7221.       else {
  7222.     U_CHAR *cp = buf;
  7223.     fprintf (pcp_outfile, "#undef ");
  7224.     while (is_idchar[*cp]) /* Ick! */
  7225.       fputc (*cp++, pcp_outfile);
  7226.     putc ('\n', pcp_outfile);
  7227.       }
  7228.     }
  7229.  
  7230.     skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
  7231.     if (start_of_file && !skip) {
  7232.       control_macro = (U_CHAR *) xmalloc (end - buf + 1);
  7233.       bcopy ((char *) buf, (char *) control_macro, end - buf);
  7234.       control_macro[end - buf] = 0;
  7235.     }
  7236.   }
  7237.   
  7238.   conditional_skip (ip, skip, T_IF, control_macro, op);
  7239.   return 0;
  7240. }
  7241.  
  7242. /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  7243.    If this is a #ifndef starting at the beginning of a file,
  7244.    CONTROL_MACRO is the macro name tested by the #ifndef.
  7245.    Otherwise, CONTROL_MACRO is 0.  */
  7246.  
  7247. static void
  7248. conditional_skip (ip, skip, type, control_macro, op)
  7249.      FILE_BUF *ip;
  7250.      int skip;
  7251.      enum node_type type;
  7252.      U_CHAR *control_macro;
  7253.      FILE_BUF *op;
  7254. {
  7255.   IF_STACK_FRAME *temp;
  7256.  
  7257.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  7258.   temp->fname = ip->nominal_fname;
  7259.   temp->lineno = ip->lineno;
  7260.   temp->next = if_stack;
  7261.   temp->control_macro = control_macro;
  7262.   if_stack = temp;
  7263.  
  7264.   if_stack->type = type;
  7265.  
  7266.   if (skip != 0) {
  7267.     skip_if_group (ip, 0, op);
  7268.     return;
  7269.   } else {
  7270.     ++if_stack->if_succeeded;
  7271.     output_line_directive (ip, &outbuf, 1, same_file);
  7272.   }
  7273. }
  7274.  
  7275. /*
  7276.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  7277.  * leaves input ptr at the sharp sign found.
  7278.  * If ANY is nonzero, return at next directive of any sort.
  7279.  */
  7280. static void
  7281. skip_if_group (ip, any, op)
  7282.      FILE_BUF *ip;
  7283.      int any;
  7284.      FILE_BUF *op;
  7285. {
  7286.   register U_CHAR *bp = ip->bufp, *cp;
  7287.   register U_CHAR *endb = ip->buf + ip->length;
  7288.   struct directive *kt;
  7289.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  7290.   U_CHAR *beg_of_line = bp;
  7291.   register int ident_length;
  7292.   U_CHAR *ident, *after_ident;
  7293.   /* Save info about where the group starts.  */
  7294.   U_CHAR *beg_of_group = bp;
  7295.   int beg_lineno = ip->lineno;
  7296.  
  7297.   if (output_conditionals && op != 0) {
  7298.     char *ptr = "#failed\n";
  7299.     int len = strlen (ptr);
  7300.  
  7301.     if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7302.       {
  7303.     *op->bufp++ = '\n';
  7304.     op->lineno++;
  7305.       }
  7306.     check_expand (op, len);
  7307.     bcopy (ptr, (char *) op->bufp, len);
  7308.     op->bufp += len;
  7309.     op->lineno++;
  7310.     output_line_directive (ip, op, 1, 0);
  7311.   }
  7312.  
  7313.   while (bp < endb) {
  7314.     switch (*bp++) {
  7315.     case '/':            /* possible comment */
  7316.       if (*bp == '\\' && bp[1] == '\n')
  7317.     newline_fix (bp);
  7318.       if (*bp == '*'
  7319.       || (cplusplus_comments && *bp == '/')) {
  7320.     ip->bufp = ++bp;
  7321.     bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
  7322.       }
  7323.       break;
  7324.     case '\"':
  7325.     case '\'':
  7326.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
  7327.                    NULL_PTR, NULL_PTR);
  7328.       break;
  7329.     case '\\':
  7330.       /* Char after backslash loses its special meaning.  */
  7331.       if (bp < endb) {
  7332.     if (*bp == '\n')
  7333.       ++ip->lineno;        /* But do update the line-count.  */
  7334.     bp++;
  7335.       }
  7336.       break;
  7337.     case '\n':
  7338.       ++ip->lineno;
  7339.       beg_of_line = bp;
  7340.       break;
  7341.     case '%':
  7342.       if (beg_of_line == 0 || traditional)
  7343.     break;
  7344.       ip->bufp = bp - 1;
  7345.       while (bp[0] == '\\' && bp[1] == '\n')
  7346.     bp += 2;
  7347.       if (*bp == ':')
  7348.     goto sharp_token;
  7349.       break;
  7350.     case '#':
  7351.       /* # keyword: a # must be first nonblank char on the line */
  7352.       if (beg_of_line == 0)
  7353.     break;
  7354.       ip->bufp = bp - 1;
  7355.     sharp_token:
  7356.       /* Scan from start of line, skipping whitespace, comments
  7357.      and backslash-newlines, and see if we reach this #.
  7358.      If not, this # is not special.  */
  7359.       bp = beg_of_line;
  7360.       /* If -traditional, require # to be at beginning of line.  */
  7361.       if (!traditional) {
  7362.     while (1) {
  7363.       if (is_hor_space[*bp])
  7364.         bp++;
  7365.       else if (*bp == '\\' && bp[1] == '\n')
  7366.         bp += 2;
  7367.       else if (*bp == '/' && bp[1] == '*') {
  7368.         bp += 2;
  7369.         while (!(*bp == '*' && bp[1] == '/'))
  7370.           bp++;
  7371.         bp += 2;
  7372.       }
  7373.       /* There is no point in trying to deal with C++ // comments here,
  7374.          because if there is one, then this # must be part of the
  7375.          comment and we would never reach here.  */
  7376.       else break;
  7377.     }
  7378.       }
  7379.       if (bp != ip->bufp) {
  7380.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  7381.     break;
  7382.       }
  7383.  
  7384.       bp = ip->bufp + 1;    /* Point after the '#' */
  7385.       if (ip->bufp[0] == '%') {
  7386.     /* Skip past the ':' again.  */
  7387.     while (*bp == '\\') {
  7388.       ip->lineno++;
  7389.       bp += 2;
  7390.     }
  7391.     bp++;
  7392.       }
  7393.  
  7394.       /* Skip whitespace and \-newline.  */
  7395.       while (1) {
  7396.     if (is_hor_space[*bp])
  7397.       bp++;
  7398.     else if (*bp == '\\' && bp[1] == '\n')
  7399.       bp += 2;
  7400.     else if (*bp == '/' && bp[1] == '*') {
  7401.       bp += 2;
  7402.       while (!(*bp == '*' && bp[1] == '/')) {
  7403.         if (*bp == '\n')
  7404.           ip->lineno++;
  7405.         bp++;
  7406.       }
  7407.       bp += 2;
  7408.     } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  7409.       bp += 2;
  7410.       while (bp[-1] == '\\' || *bp != '\n') {
  7411.         if (*bp == '\n')
  7412.           ip->lineno++;
  7413.         bp++;
  7414.       }
  7415.         }
  7416.     else break;
  7417.       }
  7418.  
  7419.       cp = bp;
  7420.  
  7421.       /* Now find end of directive name.
  7422.      If we encounter a backslash-newline, exchange it with any following
  7423.      symbol-constituents so that we end up with a contiguous name.  */
  7424.  
  7425.       while (1) {
  7426.     if (is_idchar[*bp])
  7427.       bp++;
  7428.     else {
  7429.       if (*bp == '\\' && bp[1] == '\n')
  7430.         name_newline_fix (bp);
  7431.       if (is_idchar[*bp])
  7432.         bp++;
  7433.       else break;
  7434.     }
  7435.       }
  7436.       ident_length = bp - cp;
  7437.       ident = cp;
  7438.       after_ident = bp;
  7439.  
  7440.       /* A line of just `#' becomes blank.  */
  7441.  
  7442.       if (ident_length == 0 && *after_ident == '\n') {
  7443.     continue;
  7444.       }
  7445.  
  7446.       if (ident_length == 0 || !is_idstart[*ident]) {
  7447.     U_CHAR *p = ident;
  7448.     while (is_idchar[*p]) {
  7449.       if (*p < '0' || *p > '9')
  7450.         break;
  7451.       p++;
  7452.     }
  7453.     /* Handle # followed by a line number.  */
  7454.     if (p != ident && !is_idchar[*p]) {
  7455.       if (pedantic)
  7456.         pedwarn ("`#' followed by integer");
  7457.       continue;
  7458.     }
  7459.  
  7460.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  7461.     if (p == ident) {
  7462.       while (*p == '#' || is_hor_space[*p]) p++;
  7463.       if (*p == '\n') {
  7464.         if (pedantic && !lang_asm)
  7465.           pedwarn ("invalid preprocessing directive");
  7466.         continue;
  7467.       }
  7468.     }
  7469.  
  7470.     if (!lang_asm && pedantic)
  7471.       pedwarn ("invalid preprocessing directive name");
  7472.     continue;
  7473.       }
  7474.  
  7475.       for (kt = directive_table; kt->length >= 0; kt++) {
  7476.     IF_STACK_FRAME *temp;
  7477.     if (ident_length == kt->length
  7478.         && bcmp (cp, kt->name, kt->length) == 0) {
  7479.       /* If we are asked to return on next directive, do so now.  */
  7480.       if (any)
  7481.         goto done;
  7482.  
  7483.       switch (kt->type) {
  7484.       case T_IF:
  7485.       case T_IFDEF:
  7486.       case T_IFNDEF:
  7487.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  7488.         temp->next = if_stack;
  7489.         if_stack = temp;
  7490.         temp->lineno = ip->lineno;
  7491.         temp->fname = ip->nominal_fname;
  7492.         temp->type = kt->type;
  7493.         break;
  7494.       case T_ELSE:
  7495.       case T_ENDIF:
  7496.         if (pedantic && if_stack != save_if_stack)
  7497.           validate_else (bp);
  7498.       case T_ELIF:
  7499.         if (if_stack == instack[indepth].if_stack) {
  7500.           error ("`#%s' not within a conditional", kt->name);
  7501.           break;
  7502.         }
  7503.         else if (if_stack == save_if_stack)
  7504.           goto done;        /* found what we came for */
  7505.  
  7506.         if (kt->type != T_ENDIF) {
  7507.           if (if_stack->type == T_ELSE)
  7508.         error ("`#else' or `#elif' after `#else'");
  7509.           if_stack->type = kt->type;
  7510.           break;
  7511.         }
  7512.  
  7513.         temp = if_stack;
  7514.         if_stack = if_stack->next;
  7515.         free (temp);
  7516.         break;
  7517.  
  7518.        default:
  7519.         break;
  7520.       }
  7521.       break;
  7522.     }
  7523.       }
  7524.       /* Don't let erroneous code go by.  */
  7525.       if (kt->length < 0 && !lang_asm && pedantic)
  7526.     pedwarn ("invalid preprocessing directive name");
  7527.     }
  7528.   }
  7529.  
  7530.   ip->bufp = bp;
  7531.   /* after this returns, rescan will exit because ip->bufp
  7532.      now points to the end of the buffer.
  7533.      rescan is responsible for the error message also.  */
  7534.  
  7535.  done:
  7536.   if (output_conditionals && op != 0) {
  7537.     char *ptr = "#endfailed\n";
  7538.     int len = strlen (ptr);
  7539.  
  7540.     if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7541.       {
  7542.     *op->bufp++ = '\n';
  7543.     op->lineno++;
  7544.       }
  7545.     check_expand (op, beg_of_line - beg_of_group);
  7546.     bcopy ((char *) beg_of_group, (char *) op->bufp,
  7547.        beg_of_line - beg_of_group);
  7548.     op->bufp += beg_of_line - beg_of_group;
  7549.     op->lineno += ip->lineno - beg_lineno;
  7550.     check_expand (op, len);
  7551.     bcopy (ptr, (char *) op->bufp, len);
  7552.     op->bufp += len;
  7553.     op->lineno++;
  7554.   }
  7555. }
  7556.  
  7557. /*
  7558.  * handle a #else directive.  Do this by just continuing processing
  7559.  * without changing  if_stack ;  this is so that the error message
  7560.  * for missing #endif's etc. will point to the original #if.  It
  7561.  * is possible that something different would be better.
  7562.  */
  7563.  
  7564. static int
  7565. do_else (buf, limit, op, keyword)
  7566.      U_CHAR *buf, *limit;
  7567.      FILE_BUF *op;
  7568.      struct directive *keyword;
  7569. {
  7570.   FILE_BUF *ip = &instack[indepth];
  7571.  
  7572.   if (pedantic) {
  7573.     SKIP_WHITE_SPACE (buf);
  7574.     if (buf != limit)
  7575.       pedwarn ("text following `#else' violates ANSI standard");
  7576.   }
  7577.  
  7578.   if (if_stack == instack[indepth].if_stack) {
  7579.     error ("`#else' not within a conditional");
  7580.     return 0;
  7581.   } else {
  7582.     /* #ifndef can't have its special treatment for containing the whole file
  7583.        if it has a #else clause.  */
  7584.     if_stack->control_macro = 0;
  7585.  
  7586.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  7587.       error ("`#else' after `#else'");
  7588.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  7589.       if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
  7590.     fprintf (stderr, ", file %s", if_stack->fname);
  7591.       fprintf (stderr, ")\n");
  7592.     }
  7593.     if_stack->type = T_ELSE;
  7594.   }
  7595.  
  7596.   if (if_stack->if_succeeded)
  7597.     skip_if_group (ip, 0, op);
  7598.   else {
  7599.     ++if_stack->if_succeeded;    /* continue processing input */
  7600.     output_line_directive (ip, op, 1, same_file);
  7601.   }
  7602.   return 0;
  7603. }
  7604.  
  7605. /*
  7606.  * unstack after #endif directive
  7607.  */
  7608.  
  7609. static int
  7610. do_endif (buf, limit, op, keyword)
  7611.      U_CHAR *buf, *limit;
  7612.      FILE_BUF *op;
  7613.      struct directive *keyword;
  7614. {
  7615.   if (pedantic) {
  7616.     SKIP_WHITE_SPACE (buf);
  7617.     if (buf != limit)
  7618.       pedwarn ("text following `#endif' violates ANSI standard");
  7619.   }
  7620.  
  7621.   if (if_stack == instack[indepth].if_stack)
  7622.     error ("unbalanced `#endif'");
  7623.   else {
  7624.     IF_STACK_FRAME *temp = if_stack;
  7625.     if_stack = if_stack->next;
  7626.     if (temp->control_macro != 0) {
  7627.       /* This #endif matched a #ifndef at the start of the file.
  7628.      See if it is at the end of the file.  */
  7629.       FILE_BUF *ip = &instack[indepth];
  7630.       U_CHAR *p = ip->bufp;
  7631.       U_CHAR *ep = ip->buf + ip->length;
  7632.  
  7633.       while (p != ep) {
  7634.     U_CHAR c = *p++;
  7635.     if (!is_space[c]) {
  7636.       if (c == '/'
  7637.           && (*p == '*' || (cplusplus_comments && *p == '/'))) {
  7638.         /* Skip this comment.  */
  7639.         int junk = 0;
  7640.         U_CHAR *save_bufp = ip->bufp;
  7641.         ip->bufp = p + 1;
  7642.         p = skip_to_end_of_comment (ip, &junk, 1);
  7643.         ip->bufp = save_bufp;
  7644.       } else
  7645.         goto fail;
  7646.     }
  7647.       }
  7648.       /* If we get here, this #endif ends a #ifndef
  7649.      that contains all of the file (aside from whitespace).
  7650.      Arrange not to include the file again
  7651.      if the macro that was tested is defined.
  7652.  
  7653.      Do not do this for the top-level file in a -include or any
  7654.      file in a -imacros.  */
  7655.       if (indepth != 0
  7656.       && ! (indepth == 1 && no_record_file)
  7657.       && ! (no_record_file && no_output))
  7658.     record_control_macro (ip->fname, temp->control_macro);
  7659.     fail: ;
  7660.     }
  7661.     free (temp);
  7662.     output_line_directive (&instack[indepth], op, 1, same_file);
  7663.   }
  7664.   return 0;
  7665. }
  7666.  
  7667. /* When an #else or #endif is found while skipping failed conditional,
  7668.    if -pedantic was specified, this is called to warn about text after
  7669.    the directive name.  P points to the first char after the directive name.  */
  7670.  
  7671. static void
  7672. validate_else (p)
  7673.      register U_CHAR *p;
  7674. {
  7675.   /* Advance P over whitespace and comments.  */
  7676.   while (1) {
  7677.     if (*p == '\\' && p[1] == '\n')
  7678.       p += 2;
  7679.     if (is_hor_space[*p])
  7680.       p++;
  7681.     else if (*p == '/') {
  7682.       if (p[1] == '\\' && p[2] == '\n')
  7683.     newline_fix (p + 1);
  7684.       if (p[1] == '*') {
  7685.     p += 2;
  7686.     /* Don't bother warning about unterminated comments
  7687.        since that will happen later.  Just be sure to exit.  */
  7688.     while (*p) {
  7689.       if (p[1] == '\\' && p[2] == '\n')
  7690.         newline_fix (p + 1);
  7691.       if (*p == '*' && p[1] == '/') {
  7692.         p += 2;
  7693.         break;
  7694.       }
  7695.       p++;
  7696.     }
  7697.       }
  7698.       else if (cplusplus_comments && p[1] == '/') {
  7699.     p += 2;
  7700.     while (*p && (*p != '\n' || p[-1] == '\\'))
  7701.       p++;
  7702.       }
  7703.     } else break;
  7704.   }
  7705.   if (*p && *p != '\n')
  7706.     pedwarn ("text following `#else' or `#endif' violates ANSI standard");
  7707. }
  7708.  
  7709. /* Skip a comment, assuming the input ptr immediately follows the
  7710.    initial slash-star.  Bump *LINE_COUNTER for each newline.
  7711.    (The canonical line counter is &ip->lineno.)
  7712.    Don't use this routine (or the next one) if bumping the line
  7713.    counter is not sufficient to deal with newlines in the string.
  7714.  
  7715.    If NOWARN is nonzero, don't warn about slash-star inside a comment.
  7716.    This feature is useful when processing a comment that is going to be
  7717.    processed or was processed at another point in the preprocessor,
  7718.    to avoid a duplicate warning.  Likewise for unterminated comment errors.  */
  7719.  
  7720. static U_CHAR *
  7721. skip_to_end_of_comment (ip, line_counter, nowarn)
  7722.      register FILE_BUF *ip;
  7723.      int *line_counter;        /* place to remember newlines, or NULL */
  7724.      int nowarn;
  7725. {
  7726.   register U_CHAR *limit = ip->buf + ip->length;
  7727.   register U_CHAR *bp = ip->bufp;
  7728.   FILE_BUF *op = &outbuf;    /* JF */
  7729.   int output = put_out_comments && !line_counter;
  7730.   int start_line = line_counter ? *line_counter : 0;
  7731.  
  7732.     /* JF this line_counter stuff is a crock to make sure the
  7733.        comment is only put out once, no matter how many times
  7734.        the comment is skipped.  It almost works */
  7735.   if (output) {
  7736.     *op->bufp++ = '/';
  7737.     *op->bufp++ = '*';
  7738.   }
  7739.   if (cplusplus_comments && bp[-1] == '/') {
  7740.     if (output) {
  7741.       while (bp < limit) {
  7742.     *op->bufp++ = *bp;
  7743.     if (*bp == '\n' && bp[-1] != '\\')
  7744.       break;
  7745.     if (*bp == '\n') {
  7746.       ++*line_counter;
  7747.       ++op->lineno;
  7748.     }
  7749.     bp++;
  7750.       }
  7751.       op->bufp[-1] = '*';
  7752.       *op->bufp++ = '/';
  7753.       *op->bufp++ = '\n';
  7754.     } else {
  7755.       while (bp < limit) {
  7756.     if (bp[-1] != '\\' && *bp == '\n') {
  7757.       break;
  7758.     } else {
  7759.       if (*bp == '\n' && line_counter)
  7760.         ++*line_counter;
  7761.       bp++;
  7762.     }
  7763.       }
  7764.     }
  7765.     ip->bufp = bp;
  7766.     return bp;
  7767.   }
  7768.   while (bp < limit) {
  7769.     if (output)
  7770.       *op->bufp++ = *bp;
  7771.     switch (*bp++) {
  7772.     case '/':
  7773.       if (warn_comments && !nowarn && bp < limit && *bp == '*')
  7774.     warning ("`/*' within comment");
  7775.       break;
  7776.     case '\n':
  7777.       /* If this is the end of the file, we have an unterminated comment.
  7778.      Don't swallow the newline.  We are guaranteed that there will be a
  7779.      trailing newline and various pieces assume it's there.  */
  7780.       if (bp == limit)
  7781.     {
  7782.       --bp;
  7783.       --limit;
  7784.       break;
  7785.     }
  7786.       if (line_counter != NULL)
  7787.     ++*line_counter;
  7788.       if (output)
  7789.     ++op->lineno;
  7790.       break;
  7791.     case '*':
  7792.       if (*bp == '\\' && bp[1] == '\n')
  7793.     newline_fix (bp);
  7794.       if (*bp == '/') {
  7795.         if (output)
  7796.       *op->bufp++ = '/';
  7797.     ip->bufp = ++bp;
  7798.     return bp;
  7799.       }
  7800.       break;
  7801.     }
  7802.   }
  7803.  
  7804.   if (!nowarn)
  7805.     error_with_line (line_for_error (start_line), "unterminated comment");
  7806.   ip->bufp = bp;
  7807.   return bp;
  7808. }
  7809.  
  7810. /*
  7811.  * Skip over a quoted string.  BP points to the opening quote.
  7812.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  7813.  * START_LINE is the line number of the starting point (but it need
  7814.  * not be valid if the starting point is inside a macro expansion).
  7815.  *
  7816.  * The input stack state is not changed.
  7817.  *
  7818.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  7819.  * for each newline passed.
  7820.  *
  7821.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  7822.  * if we pass a backslash-newline.
  7823.  *
  7824.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  7825.  */
  7826. static U_CHAR *
  7827. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  7828.      register U_CHAR *bp;
  7829.      register U_CHAR *limit;
  7830.      int start_line;
  7831.      int *count_newlines;
  7832.      int *backslash_newlines_p;
  7833.      int *eofp;
  7834. {
  7835.   register U_CHAR c, match;
  7836.  
  7837.   match = *bp++;
  7838.   while (1) {
  7839.     if (bp >= limit) {
  7840.       error_with_line (line_for_error (start_line),
  7841.                "unterminated string or character constant");
  7842.       error_with_line (multiline_string_line,
  7843.                "possible real start of unterminated constant");
  7844.       multiline_string_line = 0;
  7845.       if (eofp)
  7846.     *eofp = 1;
  7847.       break;
  7848.     }
  7849.     c = *bp++;
  7850.     if (c == '\\') {
  7851.       while (*bp == '\\' && bp[1] == '\n') {
  7852.     if (backslash_newlines_p)
  7853.       *backslash_newlines_p = 1;
  7854.     if (count_newlines)
  7855.       ++*count_newlines;
  7856.     bp += 2;
  7857.       }
  7858.       if (*bp == '\n' && count_newlines) {
  7859.     if (backslash_newlines_p)
  7860.       *backslash_newlines_p = 1;
  7861.     ++*count_newlines;
  7862.       }
  7863.       bp++;
  7864.     } else if (c == '\n') {
  7865.       if (traditional) {
  7866.      /* Unterminated strings and character constants are 'valid'.  */
  7867.      bp--;    /* Don't consume the newline. */
  7868.      if (eofp)
  7869.        *eofp = 1;
  7870.      break;
  7871.       }
  7872.       if (pedantic || match == '\'') {
  7873.     error_with_line (line_for_error (start_line),
  7874.              "unterminated string or character constant");
  7875.     bp--;
  7876.     if (eofp)
  7877.       *eofp = 1;
  7878.     break;
  7879.       }
  7880.       /* If not traditional, then allow newlines inside strings.  */
  7881.       if (count_newlines)
  7882.     ++*count_newlines;
  7883.       if (multiline_string_line == 0)
  7884.     multiline_string_line = start_line;
  7885.     } else if (c == match)
  7886.       break;
  7887.   }
  7888.   return bp;
  7889. }
  7890.  
  7891. /* Place into DST a quoted string representing the string SRC.
  7892.    Return the address of DST's terminating null.  */
  7893. static char *
  7894. quote_string (dst, src)
  7895.      char *dst, *src;
  7896. {
  7897.   U_CHAR c;
  7898.  
  7899.   *dst++ = '\"';
  7900.   for (;;)
  7901.     switch ((c = *src++))
  7902.       {
  7903.       default:
  7904.         if (isprint (c))
  7905.       *dst++ = c;
  7906.     else
  7907.       {
  7908.         sprintf (dst, "\\%03o", c);
  7909.         dst += 4;
  7910.       }
  7911.     break;
  7912.  
  7913.       case '\"':
  7914.       case '\\':
  7915.     *dst++ = '\\';
  7916.     *dst++ = c;
  7917.     break;
  7918.       
  7919.       case '\0':
  7920.     *dst++ = '\"';
  7921.     *dst = '\0';
  7922.     return dst;
  7923.       }
  7924. }
  7925.  
  7926. /* Skip across a group of balanced parens, starting from IP->bufp.
  7927.    IP->bufp is updated.  Use this with IP->bufp pointing at an open-paren.
  7928.  
  7929.    This does not handle newlines, because it's used for the arg of #if,
  7930.    where there aren't any newlines.  Also, backslash-newline can't appear.  */
  7931.  
  7932. static U_CHAR *
  7933. skip_paren_group (ip)
  7934.      register FILE_BUF *ip;
  7935. {
  7936.   U_CHAR *limit = ip->buf + ip->length;
  7937.   U_CHAR *p = ip->bufp;
  7938.   int depth = 0;
  7939.   int lines_dummy = 0;
  7940.  
  7941.   while (p != limit) {
  7942.     int c = *p++;
  7943.     switch (c) {
  7944.     case '(':
  7945.       depth++;
  7946.       break;
  7947.  
  7948.     case ')':
  7949.       depth--;
  7950.       if (depth == 0)
  7951.     return ip->bufp = p;
  7952.       break;
  7953.  
  7954.     case '/':
  7955.       if (*p == '*') {
  7956.     ip->bufp = p;
  7957.     p = skip_to_end_of_comment (ip, &lines_dummy, 0);
  7958.     p = ip->bufp;
  7959.       }
  7960.  
  7961.     case '"':
  7962.     case '\'':
  7963.       {
  7964.     int eofp = 0;
  7965.     p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  7966.     if (eofp)
  7967.       return ip->bufp = p;
  7968.       }
  7969.       break;
  7970.     }
  7971.   }
  7972.  
  7973.   ip->bufp = p;
  7974.   return p;
  7975. }
  7976.  
  7977. /*
  7978.  * write out a #line directive, for instance, after an #include file.
  7979.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  7980.  * appear to be a no-op, and we can output a few newlines instead
  7981.  * if we want to increase the line number by a small amount.
  7982.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  7983.  */
  7984.  
  7985. static void
  7986. output_line_directive (ip, op, conditional, file_change)
  7987.      FILE_BUF *ip, *op;
  7988.      int conditional;
  7989.      enum file_change_code file_change;
  7990. {
  7991.   int len;
  7992.   char *line_directive_buf, *line_end;
  7993.  
  7994.   if (no_line_directives
  7995.       || ip->fname == NULL
  7996.       || no_output) {
  7997.     op->lineno = ip->lineno;
  7998.     return;
  7999.   }
  8000.  
  8001.   if (conditional) {
  8002.     if (ip->lineno == op->lineno)
  8003.       return;
  8004.  
  8005.     /* If the inherited line number is a little too small,
  8006.        output some newlines instead of a #line directive.  */
  8007.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  8008.       check_expand (op, 10);
  8009.       while (ip->lineno > op->lineno) {
  8010.     *op->bufp++ = '\n';
  8011.     op->lineno++;
  8012.       }
  8013.       return;
  8014.     }
  8015.   }
  8016.  
  8017.   /* Don't output a line number of 0 if we can help it.  */
  8018.   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
  8019.       && *ip->bufp == '\n') {
  8020.     ip->lineno++;
  8021.     ip->bufp++;
  8022.   }
  8023.  
  8024.   line_directive_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100);
  8025.   sprintf (line_directive_buf, "# %d ", ip->lineno);
  8026.   line_end = quote_string (line_directive_buf + strlen (line_directive_buf),
  8027.                ip->nominal_fname);
  8028.   if (file_change != same_file) {
  8029.     *line_end++ = ' ';
  8030.     *line_end++ = file_change == enter_file ? '1' : '2';
  8031.   }
  8032.   /* Tell cc1 if following text comes from a system header file.  */
  8033.   if (ip->system_header_p) {
  8034.     *line_end++ = ' ';
  8035.     *line_end++ = '3';
  8036.   }
  8037. #ifndef NO_IMPLICIT_EXTERN_C
  8038.   /* Tell cc1plus if following text should be treated as C.  */
  8039.   if (ip->system_header_p == 2 && cplusplus) {
  8040.     *line_end++ = ' ';
  8041.     *line_end++ = '4';
  8042.   }
  8043. #endif
  8044.   *line_end++ = '\n';
  8045.   len = line_end - line_directive_buf;
  8046.   check_expand (op, len + 1);
  8047.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  8048.     *op->bufp++ = '\n';
  8049.   bcopy ((char *) line_directive_buf, (char *) op->bufp, len);
  8050.   op->bufp += len;
  8051.   op->lineno = ip->lineno;
  8052. }
  8053.  
  8054. /* This structure represents one parsed argument in a macro call.
  8055.    `raw' points to the argument text as written (`raw_length' is its length).
  8056.    `expanded' points to the argument's macro-expansion
  8057.    (its length is `expand_length').
  8058.    `stringified_length' is the length the argument would have
  8059.    if stringified.
  8060.    `use_count' is the number of times this macro arg is substituted
  8061.    into the macro.  If the actual use count exceeds 10, 
  8062.    the value stored is 10.
  8063.    `free1' and `free2', if nonzero, point to blocks to be freed
  8064.    when the macro argument data is no longer needed.  */
  8065.  
  8066. struct argdata {
  8067.   U_CHAR *raw, *expanded;
  8068.   int raw_length, expand_length;
  8069.   int stringified_length;
  8070.   U_CHAR *free1, *free2;
  8071.   char newlines;
  8072.   char use_count;
  8073. };
  8074.  
  8075. /* Expand a macro call.
  8076.    HP points to the symbol that is the macro being called.
  8077.    Put the result of expansion onto the input stack
  8078.    so that subsequent input by our caller will use it.
  8079.  
  8080.    If macro wants arguments, caller has already verified that
  8081.    an argument list follows; arguments come from the input stack.  */
  8082.  
  8083. static void
  8084. macroexpand (hp, op)
  8085.      HASHNODE *hp;
  8086.      FILE_BUF *op;
  8087. {
  8088.   int nargs;
  8089.   DEFINITION *defn = hp->value.defn;
  8090.   register U_CHAR *xbuf;
  8091.   int xbuf_len;
  8092.   int start_line = instack[indepth].lineno;
  8093.   int rest_args, rest_zero;
  8094.  
  8095.   CHECK_DEPTH (return;);
  8096.  
  8097.   /* it might not actually be a macro.  */
  8098.   if (hp->type != T_MACRO) {
  8099.     special_symbol (hp, op);
  8100.     return;
  8101.   }
  8102.  
  8103.   /* This macro is being used inside a #if, which means it must be */
  8104.   /* recorded as a precondition.  */
  8105.   if (pcp_inside_if && pcp_outfile && defn->predefined)
  8106.     dump_single_macro (hp, pcp_outfile);
  8107.   
  8108.   nargs = defn->nargs;
  8109.  
  8110.   if (nargs >= 0) {
  8111.     register int i;
  8112.     struct argdata *args;
  8113.     char *parse_error = 0;
  8114.  
  8115.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  8116.  
  8117.     for (i = 0; i < nargs; i++) {
  8118.       args[i].raw = (U_CHAR *) "";
  8119.       args[i].expanded = 0;
  8120.       args[i].raw_length = args[i].expand_length
  8121.     = args[i].stringified_length = 0;
  8122.       args[i].free1 = args[i].free2 = 0;
  8123.       args[i].use_count = 0;
  8124.     }
  8125.  
  8126.     /* Parse all the macro args that are supplied.  I counts them.
  8127.        The first NARGS args are stored in ARGS.
  8128.        The rest are discarded.
  8129.        If rest_args is set then we assume macarg absorbed the rest of the args.
  8130.        */
  8131.     i = 0;
  8132.     rest_args = 0;
  8133.     do {
  8134.       /* Discard the open-parenthesis or comma before the next arg.  */
  8135.       ++instack[indepth].bufp;
  8136.       if (rest_args)
  8137.     continue;
  8138.       if (i < nargs || (nargs == 0 && i == 0)) {
  8139.     /* if we are working on last arg which absorbs rest of args... */
  8140.     if (i == nargs - 1 && defn->rest_args)
  8141.       rest_args = 1;
  8142.     parse_error = macarg (&args[i], rest_args);
  8143.       }
  8144.       else
  8145.     parse_error = macarg (NULL_PTR, 0);
  8146.       if (parse_error) {
  8147.     error_with_line (line_for_error (start_line), parse_error);
  8148.     break;
  8149.       }
  8150.       i++;
  8151.     } while (*instack[indepth].bufp != ')');
  8152.  
  8153.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  8154.     if (i == 1) {
  8155.       register U_CHAR *bp = args[0].raw;
  8156.       register U_CHAR *lim = bp + args[0].raw_length;
  8157.       /* cpp.texi says for foo ( ) we provide one argument.
  8158.      However, if foo wants just 0 arguments, treat this as 0.  */
  8159.       if (nargs == 0)
  8160.     while (bp != lim && is_space[*bp]) bp++;
  8161.       if (bp == lim)
  8162.     i = 0;
  8163.     }
  8164.  
  8165.     /* Don't output an error message if we have already output one for
  8166.        a parse error above.  */
  8167.     rest_zero = 0;
  8168.     if (nargs == 0 && i > 0) {
  8169.       if (! parse_error)
  8170.     error ("arguments given to macro `%s'", hp->name);
  8171.     } else if (i < nargs) {
  8172.       /* traditional C allows foo() if foo wants one argument.  */
  8173.       if (nargs == 1 && i == 0 && traditional)
  8174.     ;
  8175.       /* the rest args token is allowed to absorb 0 tokens */
  8176.       else if (i == nargs - 1 && defn->rest_args)
  8177.     rest_zero = 1;
  8178.       else if (parse_error)
  8179.     ;
  8180.       else if (i == 0)
  8181.     error ("macro `%s' used without args", hp->name);
  8182.       else if (i == 1)
  8183.     error ("macro `%s' used with just one arg", hp->name);
  8184.       else
  8185.     error ("macro `%s' used with only %d args", hp->name, i);
  8186.     } else if (i > nargs) {
  8187.       if (! parse_error)
  8188.     error ("macro `%s' used with too many (%d) args", hp->name, i);
  8189.     }
  8190.  
  8191.     /* Swallow the closeparen.  */
  8192.     ++instack[indepth].bufp;
  8193.  
  8194.     /* If macro wants zero args, we parsed the arglist for checking only.
  8195.        Read directly from the macro definition.  */
  8196.     if (nargs == 0) {
  8197.       xbuf = defn->expansion;
  8198.       xbuf_len = defn->length;
  8199.     } else {
  8200.       register U_CHAR *exp = defn->expansion;
  8201.       register int offset;    /* offset in expansion,
  8202.                    copied a piece at a time */
  8203.       register int totlen;    /* total amount of exp buffer filled so far */
  8204.  
  8205.       register struct reflist *ap, *last_ap;
  8206.  
  8207.       /* Macro really takes args.  Compute the expansion of this call.  */
  8208.  
  8209.       /* Compute length in characters of the macro's expansion.
  8210.      Also count number of times each arg is used.  */
  8211.       xbuf_len = defn->length;
  8212.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  8213.     if (ap->stringify)
  8214.       xbuf_len += args[ap->argno].stringified_length;
  8215.     else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional)
  8216.       /* Add 4 for two newline-space markers to prevent
  8217.          token concatenation.  */
  8218.       xbuf_len += args[ap->argno].raw_length + 4;
  8219.     else {
  8220.       /* We have an ordinary (expanded) occurrence of the arg.
  8221.          So compute its expansion, if we have not already.  */
  8222.       if (args[ap->argno].expanded == 0) {
  8223.         FILE_BUF obuf;
  8224.         obuf = expand_to_temp_buffer (args[ap->argno].raw,
  8225.                       args[ap->argno].raw + args[ap->argno].raw_length,
  8226.                       1, 0);
  8227.  
  8228.         args[ap->argno].expanded = obuf.buf;
  8229.         args[ap->argno].expand_length = obuf.length;
  8230.         args[ap->argno].free2 = obuf.buf;
  8231.       }
  8232.  
  8233.       /* Add 4 for two newline-space markers to prevent
  8234.          token concatenation.  */
  8235.       xbuf_len += args[ap->argno].expand_length + 4;
  8236.     }
  8237.     if (args[ap->argno].use_count < 10)
  8238.       args[ap->argno].use_count++;
  8239.       }
  8240.  
  8241.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  8242.  
  8243.       /* Generate in XBUF the complete expansion
  8244.      with arguments substituted in.
  8245.      TOTLEN is the total size generated so far.
  8246.      OFFSET is the index in the definition
  8247.      of where we are copying from.  */
  8248.       offset = totlen = 0;
  8249.       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
  8250.        last_ap = ap, ap = ap->next) {
  8251.     register struct argdata *arg = &args[ap->argno];
  8252.     int count_before = totlen;
  8253.  
  8254.     /* Add chars to XBUF.  */
  8255.     for (i = 0; i < ap->nchars; i++, offset++)
  8256.       xbuf[totlen++] = exp[offset];
  8257.  
  8258.     /* If followed by an empty rest arg with concatenation,
  8259.        delete the last run of nonwhite chars.  */
  8260.     if (rest_zero && totlen > count_before
  8261.         && ((ap->rest_args && ap->raw_before != 0)
  8262.         || (last_ap != NULL && last_ap->rest_args
  8263.             && last_ap->raw_after != 0))) {
  8264.       /* Delete final whitespace.  */
  8265.       while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
  8266.         totlen--;
  8267.       }
  8268.  
  8269.       /* Delete the nonwhites before them.  */
  8270.       while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
  8271.         totlen--;
  8272.       }
  8273.     }
  8274.  
  8275.     if (ap->stringify != 0) {
  8276.       int arglen = arg->raw_length;
  8277.       int escaped = 0;
  8278.       int in_string = 0;
  8279.       int c;
  8280.       i = 0;
  8281.       while (i < arglen
  8282.          && (c = arg->raw[i], is_space[c]))
  8283.         i++;
  8284.       while (i < arglen
  8285.          && (c = arg->raw[arglen - 1], is_space[c]))
  8286.         arglen--;
  8287.       if (!traditional)
  8288.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  8289.       for (; i < arglen; i++) {
  8290.         c = arg->raw[i];
  8291.  
  8292.         /* Special markers Newline Space
  8293.            generate nothing for a stringified argument.  */
  8294.         if (c == '\n' && arg->raw[i+1] != '\n') {
  8295.           i++;
  8296.           continue;
  8297.         }
  8298.  
  8299.         /* Internal sequences of whitespace are replaced by one space
  8300.            except within an string or char token.  */
  8301.         if (! in_string
  8302.         && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
  8303.           while (1) {
  8304.         /* Note that Newline Space does occur within whitespace
  8305.            sequences; consider it part of the sequence.  */
  8306.         if (c == '\n' && is_space[arg->raw[i+1]])
  8307.           i += 2;
  8308.         else if (c != '\n' && is_space[c])
  8309.           i++;
  8310.         else break;
  8311.         c = arg->raw[i];
  8312.           }
  8313.           i--;
  8314.           c = ' ';
  8315.         }
  8316.  
  8317.         if (escaped)
  8318.           escaped = 0;
  8319.         else {
  8320.           if (c == '\\')
  8321.         escaped = 1;
  8322.           if (in_string) {
  8323.         if (c == in_string)
  8324.           in_string = 0;
  8325.           } else if (c == '\"' || c == '\'')
  8326.         in_string = c;
  8327.         }
  8328.  
  8329.         /* Escape these chars */
  8330.         if (c == '\"' || (in_string && c == '\\'))
  8331.           xbuf[totlen++] = '\\';
  8332.         if (isprint (c))
  8333.           xbuf[totlen++] = c;
  8334.         else {
  8335.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  8336.           totlen += 4;
  8337.         }
  8338.       }
  8339.       if (!traditional)
  8340.         xbuf[totlen++] = '\"'; /* insert ending quote */
  8341.     } else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional) {
  8342.       U_CHAR *p1 = arg->raw;
  8343.       U_CHAR *l1 = p1 + arg->raw_length;
  8344.       if (ap->raw_before != 0) {
  8345.         while (p1 != l1 && is_space[*p1]) p1++;
  8346.         while (p1 != l1 && is_idchar[*p1])
  8347.           xbuf[totlen++] = *p1++;
  8348.         /* Delete any no-reexpansion marker that follows
  8349.            an identifier at the beginning of the argument
  8350.            if the argument is concatenated with what precedes it.  */
  8351.         if (p1[0] == '\n' && p1[1] == '-')
  8352.           p1 += 2;
  8353.       } else if (!traditional) {
  8354.       /* Ordinary expanded use of the argument.
  8355.          Put in newline-space markers to prevent token pasting.  */
  8356.         xbuf[totlen++] = '\n';
  8357.         xbuf[totlen++] = ' ';
  8358.       }
  8359.       if (ap->raw_after != 0) {
  8360.         /* Arg is concatenated after: delete trailing whitespace,
  8361.            whitespace markers, and no-reexpansion markers.  */
  8362.         while (p1 != l1) {
  8363.           if (is_space[l1[-1]]) l1--;
  8364.           else if (l1[-1] == '-') {
  8365.         U_CHAR *p2 = l1 - 1;
  8366.         /* If a `-' is preceded by an odd number of newlines then it
  8367.            and the last newline are a no-reexpansion marker.  */
  8368.         while (p2 != p1 && p2[-1] == '\n') p2--;
  8369.         if ((l1 - 1 - p2) & 1) {
  8370.           l1 -= 2;
  8371.         }
  8372.         else break;
  8373.           }
  8374.           else break;
  8375.         }
  8376.       }
  8377.  
  8378.       bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
  8379.       totlen += l1 - p1;
  8380.       if (!traditional && ap->raw_after == 0) {
  8381.         /* Ordinary expanded use of the argument.
  8382.            Put in newline-space markers to prevent token pasting.  */
  8383.         xbuf[totlen++] = '\n';
  8384.         xbuf[totlen++] = ' ';
  8385.       }
  8386.     } else {
  8387.       /* Ordinary expanded use of the argument.
  8388.          Put in newline-space markers to prevent token pasting.  */
  8389.       if (!traditional) {
  8390.         xbuf[totlen++] = '\n';
  8391.         xbuf[totlen++] = ' ';
  8392.       }
  8393.       bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
  8394.          arg->expand_length);
  8395.       totlen += arg->expand_length;
  8396.       if (!traditional) {
  8397.         xbuf[totlen++] = '\n';
  8398.         xbuf[totlen++] = ' ';
  8399.       }
  8400.       /* If a macro argument with newlines is used multiple times,
  8401.          then only expand the newlines once.  This avoids creating output
  8402.          lines which don't correspond to any input line, which confuses
  8403.          gdb and gcov.  */
  8404.       if (arg->use_count > 1 && arg->newlines > 0) {
  8405.         /* Don't bother doing change_newlines for subsequent
  8406.            uses of arg.  */
  8407.         arg->use_count = 1;
  8408.         arg->expand_length
  8409.           = change_newlines (arg->expanded, arg->expand_length);
  8410.       }
  8411.     }
  8412.  
  8413.     if (totlen > xbuf_len)
  8414.       abort ();
  8415.       }
  8416.  
  8417.       /* if there is anything left of the definition
  8418.      after handling the arg list, copy that in too. */
  8419.  
  8420.       for (i = offset; i < defn->length; i++) {
  8421.     /* if we've reached the end of the macro */
  8422.     if (exp[i] == ')')
  8423.       rest_zero = 0;
  8424.     if (! (rest_zero && last_ap != NULL && last_ap->rest_args
  8425.            && last_ap->raw_after != 0))
  8426.       xbuf[totlen++] = exp[i];
  8427.       }
  8428.  
  8429.       xbuf[totlen] = 0;
  8430.       xbuf_len = totlen;
  8431.  
  8432.       for (i = 0; i < nargs; i++) {
  8433.     if (args[i].free1 != 0)
  8434.       free (args[i].free1);
  8435.     if (args[i].free2 != 0)
  8436.       free (args[i].free2);
  8437.       }
  8438.     }
  8439.   } else {
  8440.     xbuf = defn->expansion;
  8441.     xbuf_len = defn->length;
  8442.   }
  8443.  
  8444.   /* Now put the expansion on the input stack
  8445.      so our caller will commence reading from it.  */
  8446.   {
  8447.     register FILE_BUF *ip2;
  8448.  
  8449.     ip2 = &instack[++indepth];
  8450.  
  8451.     ip2->fname = 0;
  8452.     ip2->nominal_fname = 0;
  8453.     /* This may not be exactly correct, but will give much better error
  8454.        messages for nested macro calls than using a line number of zero.  */
  8455.     ip2->lineno = start_line;
  8456.     ip2->buf = xbuf;
  8457.     ip2->length = xbuf_len;
  8458.     ip2->bufp = xbuf;
  8459.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  8460.     ip2->macro = hp;
  8461.     ip2->if_stack = if_stack;
  8462.     ip2->system_header_p = 0;
  8463.  
  8464.     /* Recursive macro use sometimes works traditionally.
  8465.        #define foo(x,y) bar (x (y,0), y)
  8466.        foo (foo, baz)  */
  8467.  
  8468.     if (!traditional)
  8469.       hp->type = T_DISABLED;
  8470.   }
  8471. }
  8472.  
  8473. /*
  8474.  * Parse a macro argument and store the info on it into *ARGPTR.
  8475.  * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
  8476.  * Return nonzero to indicate a syntax error.
  8477.  */
  8478.  
  8479. static char *
  8480. macarg (argptr, rest_args)
  8481.      register struct argdata *argptr;
  8482.      int rest_args;
  8483. {
  8484.   FILE_BUF *ip = &instack[indepth];
  8485.   int paren = 0;
  8486.   int newlines = 0;
  8487.   int comments = 0;
  8488.   char *result = 0;
  8489.  
  8490.   /* Try to parse as much of the argument as exists at this
  8491.      input stack level.  */
  8492.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  8493.             &paren, &newlines, &comments, rest_args);
  8494.  
  8495.   /* If we find the end of the argument at this level,
  8496.      set up *ARGPTR to point at it in the input stack.  */
  8497.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  8498.       && bp != ip->buf + ip->length) {
  8499.     if (argptr != 0) {
  8500.       argptr->raw = ip->bufp;
  8501.       argptr->raw_length = bp - ip->bufp;
  8502.       argptr->newlines = newlines;
  8503.     }
  8504.     ip->bufp = bp;
  8505.   } else {
  8506.     /* This input stack level ends before the macro argument does.
  8507.        We must pop levels and keep parsing.
  8508.        Therefore, we must allocate a temporary buffer and copy
  8509.        the macro argument into it.  */
  8510.     int bufsize = bp - ip->bufp;
  8511.     int extra = newlines;
  8512.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  8513.     int final_start = 0;
  8514.  
  8515.     bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
  8516.     ip->bufp = bp;
  8517.     ip->lineno += newlines;
  8518.  
  8519.     while (bp == ip->buf + ip->length) {
  8520.       if (instack[indepth].macro == 0) {
  8521.     result = "unterminated macro call";
  8522.     break;
  8523.       }
  8524.       ip->macro->type = T_MACRO;
  8525.       if (ip->free_ptr)
  8526.     free (ip->free_ptr);
  8527.       ip = &instack[--indepth];
  8528.       newlines = 0;
  8529.       comments = 0;
  8530.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  8531.             &newlines, &comments, rest_args);
  8532.       final_start = bufsize;
  8533.       bufsize += bp - ip->bufp;
  8534.       extra += newlines;
  8535.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  8536.       bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
  8537.          bp - ip->bufp);
  8538.       ip->bufp = bp;
  8539.       ip->lineno += newlines;
  8540.     }
  8541.  
  8542.     /* Now, if arg is actually wanted, record its raw form,
  8543.        discarding comments and duplicating newlines in whatever
  8544.        part of it did not come from a macro expansion.
  8545.        EXTRA space has been preallocated for duplicating the newlines.
  8546.        FINAL_START is the index of the start of that part.  */
  8547.     if (argptr != 0) {
  8548.       argptr->raw = buffer;
  8549.       argptr->raw_length = bufsize;
  8550.       argptr->free1 = buffer;
  8551.       argptr->newlines = newlines;
  8552.       if ((newlines || comments) && ip->fname != 0)
  8553.     argptr->raw_length
  8554.       = final_start +
  8555.         discard_comments (argptr->raw + final_start,
  8556.                   argptr->raw_length - final_start,
  8557.                   newlines);
  8558.       argptr->raw[argptr->raw_length] = 0;
  8559.       if (argptr->raw_length > bufsize + extra)
  8560.     abort ();
  8561.     }
  8562.   }
  8563.  
  8564.   /* If we are not discarding this argument,
  8565.      macroexpand it and compute its length as stringified.
  8566.      All this info goes into *ARGPTR.  */
  8567.  
  8568.   if (argptr != 0) {
  8569.     register U_CHAR *buf, *lim;
  8570.     register int totlen;
  8571.  
  8572.     buf = argptr->raw;
  8573.     lim = buf + argptr->raw_length;
  8574.  
  8575.     while (buf != lim && is_space[*buf])
  8576.       buf++;
  8577.     while (buf != lim && is_space[lim[-1]])
  8578.       lim--;
  8579.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  8580.     while (buf != lim) {
  8581.       register U_CHAR c = *buf++;
  8582.       totlen++;
  8583.       /* Internal sequences of whitespace are replaced by one space
  8584.      in most cases, but not always.  So count all the whitespace
  8585.      in case we need to keep it all.  */
  8586. #if 0
  8587.       if (is_space[c])
  8588.     SKIP_ALL_WHITE_SPACE (buf);
  8589.       else
  8590. #endif
  8591.       if (c == '\"' || c == '\\') /* escape these chars */
  8592.     totlen++;
  8593.       else if (!isprint (c))
  8594.     totlen += 3;
  8595.     }
  8596.     argptr->stringified_length = totlen;
  8597.   }
  8598.   return result;
  8599. }
  8600.  
  8601. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  8602.    counting parens in *DEPTHPTR,
  8603.    and return if reach LIMIT
  8604.    or before a `)' that would make *DEPTHPTR negative
  8605.    or before a comma when *DEPTHPTR is zero.
  8606.    Single and double quotes are matched and termination
  8607.    is inhibited within them.  Comments also inhibit it.
  8608.    Value returned is pointer to stopping place.
  8609.  
  8610.    Increment *NEWLINES each time a newline is passed.
  8611.    REST_ARGS notifies macarg1 that it should absorb the rest of the args.
  8612.    Set *COMMENTS to 1 if a comment is seen.  */
  8613.  
  8614. static U_CHAR *
  8615. macarg1 (start, limit, depthptr, newlines, comments, rest_args)
  8616.      U_CHAR *start;
  8617.      register U_CHAR *limit;
  8618.      int *depthptr, *newlines, *comments;
  8619.      int rest_args;
  8620. {
  8621.   register U_CHAR *bp = start;
  8622.  
  8623.   while (bp < limit) {
  8624.     switch (*bp) {
  8625.     case '(':
  8626.       (*depthptr)++;
  8627.       break;
  8628.     case ')':
  8629.       if (--(*depthptr) < 0)
  8630.     return bp;
  8631.       break;
  8632.     case '\\':
  8633.       /* Traditionally, backslash makes following char not special.  */
  8634.       if (bp + 1 < limit && traditional)
  8635.     {
  8636.       bp++;
  8637.       /* But count source lines anyway.  */
  8638.       if (*bp == '\n')
  8639.         ++*newlines;
  8640.     }
  8641.       break;
  8642.     case '\n':
  8643.       ++*newlines;
  8644.       break;
  8645.     case '/':
  8646.       if (bp[1] == '\\' && bp[2] == '\n')
  8647.     newline_fix (bp + 1);
  8648.       if (cplusplus_comments && bp[1] == '/') {
  8649.     *comments = 1;
  8650.     bp += 2;
  8651.     while (bp < limit && (*bp != '\n' || bp[-1] == '\\')) {
  8652.       if (*bp == '\n') ++*newlines;
  8653.       bp++;
  8654.     }
  8655.     /* Now count the newline that we are about to skip.  */
  8656.     ++*newlines;
  8657.     break;
  8658.       }
  8659.       if (bp[1] != '*' || bp + 1 >= limit)
  8660.     break;
  8661.       *comments = 1;
  8662.       bp += 2;
  8663.       while (bp + 1 < limit) {
  8664.     if (bp[0] == '*'
  8665.         && bp[1] == '\\' && bp[2] == '\n')
  8666.       newline_fix (bp + 1);
  8667.     if (bp[0] == '*' && bp[1] == '/')
  8668.       break;
  8669.     if (*bp == '\n') ++*newlines;
  8670.     bp++;
  8671.       }
  8672.       break;
  8673.     case '\'':
  8674.     case '\"':
  8675.       {
  8676.     int quotec;
  8677.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  8678.       if (*bp == '\\') {
  8679.         bp++;
  8680.         if (*bp == '\n')
  8681.           ++*newlines;
  8682.         while (*bp == '\\' && bp[1] == '\n') {
  8683.           bp += 2;
  8684.         }
  8685.       } else if (*bp == '\n') {
  8686.         ++*newlines;
  8687.         if (quotec == '\'')
  8688.           break;
  8689.       }
  8690.     }
  8691.       }
  8692.       break;
  8693.     case ',':
  8694.       /* if we've returned to lowest level and we aren't absorbing all args */
  8695.       if ((*depthptr) == 0 && rest_args == 0)
  8696.     return bp;
  8697.       break;
  8698.     }
  8699.     bp++;
  8700.   }
  8701.  
  8702.   return bp;
  8703. }
  8704.  
  8705. /* Discard comments and duplicate newlines
  8706.    in the string of length LENGTH at START,
  8707.    except inside of string constants.
  8708.    The string is copied into itself with its beginning staying fixed.  
  8709.  
  8710.    NEWLINES is the number of newlines that must be duplicated.
  8711.    We assume that that much extra space is available past the end
  8712.    of the string.  */
  8713.  
  8714. static int
  8715. discard_comments (start, length, newlines)
  8716.      U_CHAR *start;
  8717.      int length;
  8718.      int newlines;
  8719. {
  8720.   register U_CHAR *ibp;
  8721.   register U_CHAR *obp;
  8722.   register U_CHAR *limit;
  8723.   register int c;
  8724.  
  8725.   /* If we have newlines to duplicate, copy everything
  8726.      that many characters up.  Then, in the second part,
  8727.      we will have room to insert the newlines
  8728.      while copying down.
  8729.      NEWLINES may actually be too large, because it counts
  8730.      newlines in string constants, and we don't duplicate those.
  8731.      But that does no harm.  */
  8732.   if (newlines > 0) {
  8733.     ibp = start + length;
  8734.     obp = ibp + newlines;
  8735.     limit = start;
  8736.     while (limit != ibp)
  8737.       *--obp = *--ibp;
  8738.   }
  8739.  
  8740.   ibp = start + newlines;
  8741.   limit = start + length + newlines;
  8742.   obp = start;
  8743.  
  8744.   while (ibp < limit) {
  8745.     *obp++ = c = *ibp++;
  8746.     switch (c) {
  8747.     case '\n':
  8748.       /* Duplicate the newline.  */
  8749.       *obp++ = '\n';
  8750.       break;
  8751.  
  8752.     case '\\':
  8753.       if (*ibp == '\n') {
  8754.     obp--;
  8755.     ibp++;
  8756.       }
  8757.       break;
  8758.  
  8759.     case '/':
  8760.       if (*ibp == '\\' && ibp[1] == '\n')
  8761.     newline_fix (ibp);
  8762.       /* Delete any comment.  */
  8763.       if (cplusplus_comments && ibp[0] == '/') {
  8764.     /* Comments are equivalent to spaces.  */
  8765.     obp[-1] = ' ';
  8766.     ibp++;
  8767.     while (ibp < limit && (*ibp != '\n' || ibp[-1] == '\\'))
  8768.       ibp++;
  8769.     break;
  8770.       }
  8771.       if (ibp[0] != '*' || ibp + 1 >= limit)
  8772.     break;
  8773.       /* Comments are equivalent to spaces.
  8774.      For -traditional, a comment is equivalent to nothing.  */
  8775.       if (traditional)
  8776.     obp--;
  8777.       else
  8778.     obp[-1] = ' ';
  8779.       ibp++;
  8780.       while (ibp + 1 < limit) {
  8781.     if (ibp[0] == '*'
  8782.         && ibp[1] == '\\' && ibp[2] == '\n')
  8783.       newline_fix (ibp + 1);
  8784.     if (ibp[0] == '*' && ibp[1] == '/')
  8785.       break;
  8786.     ibp++;
  8787.       }
  8788.       ibp += 2;
  8789.       break;
  8790.  
  8791.     case '\'':
  8792.     case '\"':
  8793.       /* Notice and skip strings, so that we don't
  8794.      think that comments start inside them,
  8795.      and so we don't duplicate newlines in them.  */
  8796.       {
  8797.     int quotec = c;
  8798.     while (ibp < limit) {
  8799.       *obp++ = c = *ibp++;
  8800.       if (c == quotec)
  8801.         break;
  8802.       if (c == '\n' && quotec == '\'')
  8803.         break;
  8804.       if (c == '\\' && ibp < limit) {
  8805.         while (*ibp == '\\' && ibp[1] == '\n')
  8806.           ibp += 2;
  8807.         *obp++ = *ibp++;
  8808.       }
  8809.     }
  8810.       }
  8811.       break;
  8812.     }
  8813.   }
  8814.  
  8815.   return obp - start;
  8816. }
  8817.  
  8818. /* Turn newlines to spaces in the string of length LENGTH at START,
  8819.    except inside of string constants.
  8820.    The string is copied into itself with its beginning staying fixed.  */
  8821.  
  8822. static int
  8823. change_newlines (start, length)
  8824.      U_CHAR *start;
  8825.      int length;
  8826. {
  8827.   register U_CHAR *ibp;
  8828.   register U_CHAR *obp;
  8829.   register U_CHAR *limit;
  8830.   register int c;
  8831.  
  8832.   ibp = start;
  8833.   limit = start + length;
  8834.   obp = start;
  8835.  
  8836.   while (ibp < limit) {
  8837.     *obp++ = c = *ibp++;
  8838.     switch (c) {
  8839.     case '\n':
  8840.       /* If this is a NEWLINE NEWLINE, then this is a real newline in the
  8841.      string.  Skip past the newline and its duplicate.
  8842.      Put a space in the output.  */
  8843.       if (*ibp == '\n')
  8844.     {
  8845.       ibp++;
  8846.       obp--;
  8847.       *obp++ = ' ';
  8848.     }
  8849.       break;
  8850.  
  8851.     case '\'':
  8852.     case '\"':
  8853.       /* Notice and skip strings, so that we don't delete newlines in them.  */
  8854.       {
  8855.     int quotec = c;
  8856.     while (ibp < limit) {
  8857.       *obp++ = c = *ibp++;
  8858.       if (c == quotec)
  8859.         break;
  8860.       if (c == '\n' && quotec == '\'')
  8861.         break;
  8862.     }
  8863.       }
  8864.       break;
  8865.     }
  8866.   }
  8867.  
  8868.   return obp - start;
  8869. }
  8870.  
  8871. /*
  8872.  * my_strerror - return the descriptive text associated with an `errno' code.
  8873.  */
  8874.  
  8875. char *
  8876. my_strerror (errnum)
  8877.      int errnum;
  8878. {
  8879.   char *result;
  8880.  
  8881. #ifndef VMS
  8882. #ifndef HAVE_STRERROR
  8883.   result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
  8884. #else
  8885.   result = strerror (errnum);
  8886. #endif
  8887. #else    /* VMS */
  8888.   /* VAXCRTL's strerror() takes an optional second argument, which only
  8889.      matters when the first argument is EVMSERR.  However, it's simplest
  8890.      just to pass it unconditionally.  `vaxc$errno' is declared in
  8891.      <errno.h>, and maintained by the library in parallel with `errno'.
  8892.      We assume that caller's `errnum' either matches the last setting of
  8893.      `errno' by the library or else does not have the value `EVMSERR'.  */
  8894.  
  8895.   result = strerror (errnum, vaxc$errno);
  8896. #endif
  8897.  
  8898.   if (!result)
  8899.     result = "undocumented I/O error";
  8900.  
  8901.   return result;
  8902. }
  8903.  
  8904. /*
  8905.  * error - print error message and increment count of errors.
  8906.  */
  8907.  
  8908. void
  8909. error (PRINTF_ALIST (msg))
  8910.      PRINTF_DCL (msg)
  8911. {
  8912.   va_list args;
  8913.  
  8914.   VA_START (args, msg);
  8915.   verror (msg, args);
  8916.   va_end (args);
  8917. }
  8918.  
  8919. static void
  8920. verror (msg, args)
  8921.      char *msg;
  8922.      va_list args;
  8923. {
  8924.   int i;
  8925.   FILE_BUF *ip = NULL;
  8926.  
  8927.   print_containing_files ();
  8928.  
  8929.   for (i = indepth; i >= 0; i--)
  8930.     if (instack[i].fname != NULL) {
  8931.       ip = &instack[i];
  8932.       break;
  8933.     }
  8934.  
  8935.   if (ip != NULL)
  8936.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8937.   vfprintf (stderr, msg, args);
  8938.   fprintf (stderr, "\n");
  8939.   errors++;
  8940. }
  8941.  
  8942. /* Error including a message from `errno'.  */
  8943.  
  8944. static void
  8945. error_from_errno (name)
  8946.      char *name;
  8947. {
  8948.   int i;
  8949.   FILE_BUF *ip = NULL;
  8950.  
  8951.   print_containing_files ();
  8952.  
  8953.   for (i = indepth; i >= 0; i--)
  8954.     if (instack[i].fname != NULL) {
  8955.       ip = &instack[i];
  8956.       break;
  8957.     }
  8958.  
  8959.   if (ip != NULL)
  8960.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8961.  
  8962.   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
  8963.  
  8964.   errors++;
  8965. }
  8966.  
  8967. /* Print error message but don't count it.  */
  8968.  
  8969. void
  8970. warning (PRINTF_ALIST (msg))
  8971.      PRINTF_DCL (msg)
  8972. {
  8973.   va_list args;
  8974.  
  8975.   VA_START (args, msg);
  8976.   vwarning (msg, args);
  8977.   va_end (args);
  8978. }
  8979.  
  8980. static void
  8981. vwarning (msg, args)
  8982.      char *msg;
  8983.      va_list args;
  8984. {
  8985.   int i;
  8986.   FILE_BUF *ip = NULL;
  8987.  
  8988.   if (inhibit_warnings)
  8989.     return;
  8990.  
  8991.   if (warnings_are_errors)
  8992.     errors++;
  8993.  
  8994.   print_containing_files ();
  8995.  
  8996.   for (i = indepth; i >= 0; i--)
  8997.     if (instack[i].fname != NULL) {
  8998.       ip = &instack[i];
  8999.       break;
  9000.     }
  9001.  
  9002.   if (ip != NULL)
  9003.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  9004.   fprintf (stderr, "warning: ");
  9005.   vfprintf (stderr, msg, args);
  9006.   fprintf (stderr, "\n");
  9007. }
  9008.  
  9009. static void
  9010. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9011. error_with_line (int line, PRINTF_ALIST (msg))
  9012. #else
  9013. error_with_line (line, PRINTF_ALIST (msg))
  9014.      int line;
  9015.      PRINTF_DCL (msg)
  9016. #endif
  9017. {
  9018.   va_list args;
  9019.  
  9020.   VA_START (args, msg);
  9021.   verror_with_line (line, msg, args);
  9022.   va_end (args);
  9023. }
  9024.  
  9025. static void
  9026. verror_with_line (line, msg, args)
  9027.      int line;
  9028.      char *msg;
  9029.      va_list args;
  9030. {
  9031.   int i;
  9032.   FILE_BUF *ip = NULL;
  9033.  
  9034.   print_containing_files ();
  9035.  
  9036.   for (i = indepth; i >= 0; i--)
  9037.     if (instack[i].fname != NULL) {
  9038.       ip = &instack[i];
  9039.       break;
  9040.     }
  9041.  
  9042.   if (ip != NULL)
  9043.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  9044.   vfprintf (stderr, msg, args);
  9045.   fprintf (stderr, "\n");
  9046.   errors++;
  9047. }
  9048.  
  9049. static void
  9050. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9051. warning_with_line (int line, PRINTF_ALIST (msg))
  9052. #else
  9053. warning_with_line (line, PRINTF_ALIST (msg))
  9054.      int line;
  9055.      PRINTF_DCL (msg)
  9056. #endif
  9057. {
  9058.   va_list args;
  9059.  
  9060.   VA_START (args, msg);
  9061.   vwarning_with_line (line, msg, args);
  9062.   va_end (args);
  9063. }
  9064.  
  9065. static void
  9066. vwarning_with_line (line, msg, args)
  9067.      int line;
  9068.      char *msg;
  9069.      va_list args;
  9070. {
  9071.   int i;
  9072.   FILE_BUF *ip = NULL;
  9073.  
  9074.   if (inhibit_warnings)
  9075.     return;
  9076.  
  9077.   if (warnings_are_errors)
  9078.     errors++;
  9079.  
  9080.   print_containing_files ();
  9081.  
  9082.   for (i = indepth; i >= 0; i--)
  9083.     if (instack[i].fname != NULL) {
  9084.       ip = &instack[i];
  9085.       break;
  9086.     }
  9087.  
  9088.   if (ip != NULL)
  9089.     fprintf (stderr, line ? "%s:%d: " : "%s: ", ip->nominal_fname, line);
  9090.   fprintf (stderr, "warning: ");
  9091.   vfprintf (stderr, msg, args);
  9092.   fprintf (stderr, "\n");
  9093. }
  9094.  
  9095. /* print an error message and maybe count it.  */
  9096.  
  9097. void
  9098. pedwarn (PRINTF_ALIST (msg))
  9099.      PRINTF_DCL (msg)
  9100. {
  9101.   va_list args;
  9102.  
  9103.   VA_START (args, msg);
  9104.   if (pedantic_errors)
  9105.     verror (msg, args);
  9106.   else
  9107.     vwarning (msg, args);
  9108.   va_end (args);
  9109. }
  9110.  
  9111. void
  9112. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9113. pedwarn_with_line (int line, PRINTF_ALIST (msg))
  9114. #else
  9115. pedwarn_with_line (line, PRINTF_ALIST (msg))
  9116.      int line;
  9117.      PRINTF_DCL (msg)
  9118. #endif
  9119. {
  9120.   va_list args;
  9121.  
  9122.   VA_START (args, msg);
  9123.   if (pedantic_errors)
  9124.     verror_with_line (line, msg, args);
  9125.   else
  9126.     vwarning_with_line (line, msg, args);
  9127.   va_end (args);
  9128. }
  9129.  
  9130. /* Report a warning (or an error if pedantic_errors)
  9131.    giving specified file name and line number, not current.  */
  9132.  
  9133. static void
  9134. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9135. pedwarn_with_file_and_line (char *file, int line, PRINTF_ALIST (msg))
  9136. #else
  9137. pedwarn_with_file_and_line (file, line, PRINTF_ALIST (msg))
  9138.      char *file;
  9139.      int line;
  9140.      PRINTF_DCL (msg)
  9141. #endif
  9142. {
  9143.   va_list args;
  9144.  
  9145.   if (!pedantic_errors && inhibit_warnings)
  9146.     return;
  9147.   if (file != NULL)
  9148.     fprintf (stderr, "%s:%d: ", file, line);
  9149.   if (pedantic_errors)
  9150.     errors++;
  9151.   if (!pedantic_errors)
  9152.     fprintf (stderr, "warning: ");
  9153.   VA_START (args, msg);
  9154.   vfprintf (stderr, msg, args);
  9155.   va_end (args);
  9156.   fprintf (stderr, "\n");
  9157. }
  9158.  
  9159. /* Print the file names and line numbers of the #include
  9160.    directives which led to the current file.  */
  9161.  
  9162. static void
  9163. print_containing_files ()
  9164. {
  9165.   FILE_BUF *ip = NULL;
  9166.   int i;
  9167.   int first = 1;
  9168.  
  9169.   /* If stack of files hasn't changed since we last printed
  9170.      this info, don't repeat it.  */
  9171.   if (last_error_tick == input_file_stack_tick)
  9172.     return;
  9173.  
  9174.   for (i = indepth; i >= 0; i--)
  9175.     if (instack[i].fname != NULL) {
  9176.       ip = &instack[i];
  9177.       break;
  9178.     }
  9179.  
  9180.   /* Give up if we don't find a source file.  */
  9181.   if (ip == NULL)
  9182.     return;
  9183.  
  9184.   /* Find the other, outer source files.  */
  9185.   for (i--; i >= 0; i--)
  9186.     if (instack[i].fname != NULL) {
  9187.       ip = &instack[i];
  9188.       if (first) {
  9189.     first = 0;
  9190.     fprintf (stderr, "In file included");
  9191.       } else {
  9192.     fprintf (stderr, ",\n                ");
  9193.       }
  9194.  
  9195.       fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
  9196.     }
  9197.   if (! first)
  9198.     fprintf (stderr, ":\n");
  9199.  
  9200.   /* Record we have printed the status as of this time.  */
  9201.   last_error_tick = input_file_stack_tick;
  9202. }
  9203.  
  9204. /* Return the line at which an error occurred.
  9205.    The error is not necessarily associated with the current spot
  9206.    in the input stack, so LINE says where.  LINE will have been
  9207.    copied from ip->lineno for the current input level.
  9208.    If the current level is for a file, we return LINE.
  9209.    But if the current level is not for a file, LINE is meaningless.
  9210.    In that case, we return the lineno of the innermost file.  */
  9211.  
  9212. static int
  9213. line_for_error (line)
  9214.      int line;
  9215. {
  9216.   int i;
  9217.   int line1 = line;
  9218.  
  9219.   for (i = indepth; i >= 0; ) {
  9220.     if (instack[i].fname != 0)
  9221.       return line1;
  9222.     i--;
  9223.     if (i < 0)
  9224.       return 0;
  9225.     line1 = instack[i].lineno;
  9226.   }
  9227.   abort ();
  9228.   /*NOTREACHED*/
  9229.   return 0;
  9230. }
  9231.  
  9232. /*
  9233.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  9234.  *
  9235.  * As things stand, nothing is ever placed in the output buffer to be
  9236.  * removed again except when it's KNOWN to be part of an identifier,
  9237.  * so flushing and moving down everything left, instead of expanding,
  9238.  * should work ok.
  9239.  */
  9240.  
  9241. /* You might think void was cleaner for the return type,
  9242.    but that would get type mismatch in check_expand in strict ANSI.  */
  9243. static int
  9244. grow_outbuf (obuf, needed)
  9245.      register FILE_BUF *obuf;
  9246.      register int needed;
  9247. {
  9248.   register U_CHAR *p;
  9249.   int minsize;
  9250.  
  9251.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  9252.     return 0;
  9253.  
  9254.   /* Make it at least twice as big as it is now.  */
  9255.   obuf->length *= 2;
  9256.   /* Make it have at least 150% of the free space we will need.  */
  9257.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  9258.   if (minsize > obuf->length)
  9259.     obuf->length = minsize;
  9260.  
  9261.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  9262.     memory_full ();
  9263.  
  9264.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  9265.   obuf->buf = p;
  9266.  
  9267.   return 0;
  9268. }
  9269.  
  9270. /* Symbol table for macro names and special symbols */
  9271.  
  9272. /*
  9273.  * install a name in the main hash table, even if it is already there.
  9274.  *   name stops with first non alphanumeric, except leading '#'.
  9275.  * caller must check against redefinition if that is desired.
  9276.  * delete_macro () removes things installed by install () in fifo order.
  9277.  * this is important because of the `defined' special symbol used
  9278.  * in #if, and also if pushdef/popdef directives are ever implemented.
  9279.  *
  9280.  * If LEN is >= 0, it is the length of the name.
  9281.  * Otherwise, compute the length by scanning the entire name.
  9282.  *
  9283.  * If HASH is >= 0, it is the precomputed hash code.
  9284.  * Otherwise, compute the hash code.
  9285.  */
  9286. static HASHNODE *
  9287. install (name, len, type, value, hash)
  9288.      U_CHAR *name;
  9289.      int len;
  9290.      enum node_type type;
  9291.      char *value;
  9292.      int hash;
  9293. {
  9294.   register HASHNODE *hp;
  9295.   register int i, bucket;
  9296.   register U_CHAR *p, *q;
  9297.  
  9298.   if (len < 0) {
  9299.     p = name;
  9300.     while (is_idchar[*p])
  9301.       p++;
  9302.     len = p - name;
  9303.   }
  9304.  
  9305.   if (hash < 0)
  9306.     hash = hashf (name, len, HASHSIZE);
  9307.  
  9308.   i = sizeof (HASHNODE) + len + 1;
  9309.   hp = (HASHNODE *) xmalloc (i);
  9310.   bucket = hash;
  9311.   hp->bucket_hdr = &hashtab[bucket];
  9312.   hp->next = hashtab[bucket];
  9313.   hashtab[bucket] = hp;
  9314.   hp->prev = NULL;
  9315.   if (hp->next != NULL)
  9316.     hp->next->prev = hp;
  9317.   hp->type = type;
  9318.   hp->length = len;
  9319.   hp->value.cpval = value;
  9320.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  9321.   p = hp->name;
  9322.   q = name;
  9323.   for (i = 0; i < len; i++)
  9324.     *p++ = *q++;
  9325.   hp->name[len] = 0;
  9326.   return hp;
  9327. }
  9328.  
  9329. /*
  9330.  * find the most recent hash node for name name (ending with first
  9331.  * non-identifier char) installed by install
  9332.  *
  9333.  * If LEN is >= 0, it is the length of the name.
  9334.  * Otherwise, compute the length by scanning the entire name.
  9335.  *
  9336.  * If HASH is >= 0, it is the precomputed hash code.
  9337.  * Otherwise, compute the hash code.
  9338.  */
  9339. HASHNODE *
  9340. lookup (name, len, hash)
  9341.      U_CHAR *name;
  9342.      int len;
  9343.      int hash;
  9344. {
  9345.   register U_CHAR *bp;
  9346.   register HASHNODE *bucket;
  9347.  
  9348.   if (len < 0) {
  9349.     for (bp = name; is_idchar[*bp]; bp++) ;
  9350.     len = bp - name;
  9351.   }
  9352.  
  9353.   if (hash < 0)
  9354.     hash = hashf (name, len, HASHSIZE);
  9355.  
  9356.   bucket = hashtab[hash];
  9357.   while (bucket) {
  9358.     if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
  9359.       return bucket;
  9360.     bucket = bucket->next;
  9361.   }
  9362.   return NULL;
  9363. }
  9364.  
  9365. /*
  9366.  * Delete a hash node.  Some weirdness to free junk from macros.
  9367.  * More such weirdness will have to be added if you define more hash
  9368.  * types that need it.
  9369.  */
  9370.  
  9371. /* Note that the DEFINITION of a macro is removed from the hash table
  9372.    but its storage is not freed.  This would be a storage leak
  9373.    except that it is not reasonable to keep undefining and redefining
  9374.    large numbers of macros many times.
  9375.    In any case, this is necessary, because a macro can be #undef'd
  9376.    in the middle of reading the arguments to a call to it.
  9377.    If #undef freed the DEFINITION, that would crash.  */
  9378.  
  9379. static void
  9380. delete_macro (hp)
  9381.      HASHNODE *hp;
  9382. {
  9383.  
  9384.   if (hp->prev != NULL)
  9385.     hp->prev->next = hp->next;
  9386.   if (hp->next != NULL)
  9387.     hp->next->prev = hp->prev;
  9388.  
  9389.   /* make sure that the bucket chain header that
  9390.      the deleted guy was on points to the right thing afterwards. */
  9391.   if (hp == *hp->bucket_hdr)
  9392.     *hp->bucket_hdr = hp->next;
  9393.  
  9394. #if 0
  9395.   if (hp->type == T_MACRO) {
  9396.     DEFINITION *d = hp->value.defn;
  9397.     struct reflist *ap, *nextap;
  9398.  
  9399.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  9400.       nextap = ap->next;
  9401.       free (ap);
  9402.     }
  9403.     free (d);
  9404.   }
  9405. #endif
  9406.   free (hp);
  9407. }
  9408.  
  9409. /*
  9410.  * return hash function on name.  must be compatible with the one
  9411.  * computed a step at a time, elsewhere
  9412.  */
  9413. static int
  9414. hashf (name, len, hashsize)
  9415.      register U_CHAR *name;
  9416.      register int len;
  9417.      int hashsize;
  9418. {
  9419.   register int r = 0;
  9420.  
  9421.   while (len--)
  9422.     r = HASHSTEP (r, *name++);
  9423.  
  9424.   return MAKE_POS (r) % hashsize;
  9425. }
  9426.  
  9427.  
  9428. /* Dump the definition of a single macro HP to OF.  */
  9429. static void
  9430. dump_single_macro (hp, of)
  9431.      register HASHNODE *hp;
  9432.      FILE *of;
  9433. {
  9434.   register DEFINITION *defn = hp->value.defn;
  9435.   struct reflist *ap;
  9436.   int offset;
  9437.   int concat;
  9438.  
  9439.  
  9440.   /* Print the definition of the macro HP.  */
  9441.  
  9442.   fprintf (of, "#define %s", hp->name);
  9443.  
  9444.   if (defn->nargs >= 0) {
  9445.     int i;
  9446.  
  9447.     fprintf (of, "(");
  9448.     for (i = 0; i < defn->nargs; i++) {
  9449.       dump_arg_n (defn, i, of);
  9450.       if (i + 1 < defn->nargs)
  9451.     fprintf (of, ", ");
  9452.     }
  9453.     fprintf (of, ")");
  9454.   }
  9455.  
  9456.   fprintf (of, " ");
  9457.  
  9458.   offset = 0;
  9459.   concat = 0;
  9460.   for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  9461.     dump_defn_1 (defn->expansion, offset, ap->nchars, of);
  9462.     offset += ap->nchars;
  9463.     if (!traditional) {
  9464.       if (ap->nchars != 0)
  9465.     concat = 0;
  9466.       if (ap->stringify) {
  9467.     switch (ap->stringify) {
  9468.      case SHARP_TOKEN: fprintf (of, "#"); break;
  9469.      case WHITE_SHARP_TOKEN: fprintf (of, "# "); break;
  9470.      case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break;
  9471.      case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break;
  9472.      default: abort ();
  9473.     }
  9474.       }
  9475.       if (ap->raw_before != 0) {
  9476.     if (concat) {
  9477.       switch (ap->raw_before) {
  9478.        case WHITE_SHARP_TOKEN:
  9479.        case WHITE_PERCENT_COLON_TOKEN:
  9480.         fprintf (of, " ");
  9481.         break;
  9482.        default:
  9483.         break;
  9484.       }
  9485.     } else {
  9486.       switch (ap->raw_before) {
  9487.        case SHARP_TOKEN: fprintf (of, "##"); break;
  9488.        case WHITE_SHARP_TOKEN: fprintf (of, "## "); break;
  9489.        case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
  9490.        case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break;
  9491.        default: abort ();
  9492.       }
  9493.     }
  9494.       }
  9495.       concat = 0;
  9496.     }
  9497.     dump_arg_n (defn, ap->argno, of);
  9498.     if (!traditional && ap->raw_after != 0) {
  9499.       switch (ap->raw_after) {
  9500.        case SHARP_TOKEN: fprintf (of, "##"); break;
  9501.        case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break;
  9502.        case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
  9503.        case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break;
  9504.        default: abort ();
  9505.       }
  9506.       concat = 1;
  9507.     }
  9508.   }
  9509.   dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
  9510.   fprintf (of, "\n");
  9511. }
  9512.  
  9513. /* Dump all macro definitions as #defines to stdout.  */
  9514.  
  9515. static void
  9516. dump_all_macros ()
  9517. {
  9518.   int bucket;
  9519.  
  9520.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  9521.     register HASHNODE *hp;
  9522.  
  9523.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  9524.       if (hp->type == T_MACRO)
  9525.     dump_single_macro (hp, stdout);
  9526.     }
  9527.   }
  9528. }
  9529.  
  9530. /* Output to OF a substring of a macro definition.
  9531.    BASE is the beginning of the definition.
  9532.    Output characters START thru LENGTH.
  9533.    Unless traditional, discard newlines outside of strings, thus
  9534.    converting funny-space markers to ordinary spaces.  */
  9535.  
  9536. static void
  9537. dump_defn_1 (base, start, length, of)
  9538.      U_CHAR *base;
  9539.      int start;
  9540.      int length;
  9541.      FILE *of;
  9542. {
  9543.   U_CHAR *p = base + start;
  9544.   U_CHAR *limit = base + start + length;
  9545.  
  9546.   if (traditional)
  9547.     fwrite (p, sizeof (*p), length, of);
  9548.   else {
  9549.     while (p < limit) {
  9550.       if (*p == '\"' || *p =='\'') {
  9551.     U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
  9552.                      NULL_PTR, NULL_PTR);
  9553.     fwrite (p, sizeof (*p), p1 - p, of);
  9554.     p = p1;
  9555.       } else {
  9556.     if (*p != '\n')
  9557.       putc (*p, of);
  9558.     p++;
  9559.       }
  9560.     }
  9561.   }
  9562. }
  9563.  
  9564. /* Print the name of argument number ARGNUM of macro definition DEFN
  9565.    to OF.
  9566.    Recall that DEFN->args.argnames contains all the arg names
  9567.    concatenated in reverse order with comma-space in between.  */
  9568.  
  9569. static void
  9570. dump_arg_n (defn, argnum, of)
  9571.      DEFINITION *defn;
  9572.      int argnum;
  9573.      FILE *of;
  9574. {
  9575.   register U_CHAR *p = defn->args.argnames;
  9576.   while (argnum + 1 < defn->nargs) {
  9577.     p = (U_CHAR *) index ((char *) p, ' ') + 1;
  9578.     argnum++;
  9579.   }
  9580.  
  9581.   while (*p && *p != ',') {
  9582.     putc (*p, of);
  9583.     p++;
  9584.   }
  9585. }
  9586.  
  9587. /* Initialize syntactic classifications of characters.  */
  9588.  
  9589. static void
  9590. initialize_char_syntax ()
  9591. {
  9592.   register int i;
  9593.  
  9594.   /*
  9595.    * Set up is_idchar and is_idstart tables.  These should be
  9596.    * faster than saying (is_alpha (c) || c == '_'), etc.
  9597.    * Set up these things before calling any routines tthat
  9598.    * refer to them.
  9599.    */
  9600.   for (i = 'a'; i <= 'z'; i++) {
  9601.     is_idchar[i - 'a' + 'A'] = 1;
  9602.     is_idchar[i] = 1;
  9603.     is_idstart[i - 'a' + 'A'] = 1;
  9604.     is_idstart[i] = 1;
  9605.   }
  9606.   for (i = '0'; i <= '9'; i++)
  9607.     is_idchar[i] = 1;
  9608.   is_idchar['_'] = 1;
  9609.   is_idstart['_'] = 1;
  9610.   is_idchar['$'] = dollars_in_ident;
  9611.   is_idstart['$'] = dollars_in_ident;
  9612.  
  9613.   /* horizontal space table */
  9614.   is_hor_space[' '] = 1;
  9615.   is_hor_space['\t'] = 1;
  9616.   is_hor_space['\v'] = 1;
  9617.   is_hor_space['\f'] = 1;
  9618.   is_hor_space['\r'] = 1;
  9619.  
  9620.   is_space[' '] = 1;
  9621.   is_space['\t'] = 1;
  9622.   is_space['\v'] = 1;
  9623.   is_space['\f'] = 1;
  9624.   is_space['\n'] = 1;
  9625.   is_space['\r'] = 1;
  9626.  
  9627.   char_name['\v'] = "vertical tab";
  9628.   char_name['\f'] = "formfeed";
  9629.   char_name['\r'] = "carriage return";
  9630. }
  9631.  
  9632. /* Initialize the built-in macros.  */
  9633.  
  9634. static void
  9635. initialize_builtins (inp, outp)
  9636.      FILE_BUF *inp;
  9637.      FILE_BUF *outp;
  9638. {
  9639.   install ((U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1);
  9640.   install ((U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1);
  9641.   install ((U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
  9642.   install ((U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
  9643.   install ((U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
  9644.  
  9645.   /* olsen: for SAS/C we fake a compiler version number and don't want
  9646.    *        this string to override it.
  9647.    */
  9648. #ifndef __SASC
  9649.   install ((U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
  9650. #endif /* __SASC */
  9651.  
  9652. #ifndef NO_BUILTIN_SIZE_TYPE
  9653.   install ((U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
  9654. #endif
  9655. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  9656.   install ((U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1);
  9657. #endif
  9658.   install ((U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1);
  9659.   install ((U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE,
  9660.        NULL_PTR, -1);
  9661.   install ((U_CHAR *) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE,
  9662.        NULL_PTR, -1);
  9663.   install ((U_CHAR *) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE,
  9664.        NULL_PTR, -1);
  9665.   install ((U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1);
  9666.   if (!traditional) {
  9667.     install ((U_CHAR *) "__STDC__", -1, T_CONST, "1", -1);
  9668.     install ((U_CHAR *) "__STDC_VERSION__", -1, T_CONST, "199409L", -1);
  9669.   }
  9670.   if (objc)
  9671.     install ((U_CHAR *) "__OBJC__", -1, T_CONST, "1", -1);
  9672. /*  This is supplied using a -D by the compiler driver
  9673.     so that it is present only when truly compiling with GNU C.  */
  9674. /*  install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1);  */
  9675.  
  9676.   if (debug_output)
  9677.     {
  9678.       char directive[2048];
  9679.       U_CHAR *udirective = (U_CHAR *) directive;
  9680.       register struct directive *dp = &directive_table[0];
  9681.       struct tm *timebuf = timestamp ();
  9682.  
  9683.       sprintf (directive, " __BASE_FILE__ \"%s\"\n",
  9684.            instack[0].nominal_fname);
  9685.       output_line_directive (inp, outp, 0, same_file);
  9686.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9687.                outp, dp);
  9688.  
  9689.       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
  9690.       output_line_directive (inp, outp, 0, same_file);
  9691.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9692.                outp, dp);
  9693.  
  9694. #ifndef NO_BUILTIN_SIZE_TYPE
  9695.       sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
  9696.       output_line_directive (inp, outp, 0, same_file);
  9697.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9698.                outp, dp);
  9699. #endif
  9700.  
  9701. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  9702.       sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
  9703.       output_line_directive (inp, outp, 0, same_file);
  9704.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9705.                outp, dp);
  9706. #endif
  9707.  
  9708.       sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
  9709.       output_line_directive (inp, outp, 0, same_file);
  9710.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9711.                outp, dp);
  9712.  
  9713.       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
  9714.            monthnames[timebuf->tm_mon],
  9715.            timebuf->tm_mday, timebuf->tm_year + 1900);
  9716.       output_line_directive (inp, outp, 0, same_file);
  9717.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9718.                outp, dp);
  9719.  
  9720.       sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
  9721.            timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
  9722.       output_line_directive (inp, outp, 0, same_file);
  9723.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9724.                outp, dp);
  9725.  
  9726.       if (!traditional)
  9727.     {
  9728.           sprintf (directive, " __STDC__ 1");
  9729.           output_line_directive (inp, outp, 0, same_file);
  9730.           pass_thru_directive (udirective, &udirective[strlen (directive)],
  9731.                    outp, dp);
  9732.     }
  9733.       if (objc)
  9734.     {
  9735.           sprintf (directive, " __OBJC__ 1");
  9736.           output_line_directive (inp, outp, 0, same_file);
  9737.           pass_thru_directive (udirective, &udirective[strlen (directive)],
  9738.                    outp, dp);
  9739.     }
  9740.     }
  9741. }
  9742.  
  9743. /*
  9744.  * process a given definition string, for initialization
  9745.  * If STR is just an identifier, define it with value 1.
  9746.  * If STR has anything after the identifier, then it should
  9747.  * be identifier=definition.
  9748.  */
  9749.  
  9750. static void
  9751. make_definition (str, op)
  9752.      char *str;
  9753.      FILE_BUF *op;
  9754. {
  9755.   FILE_BUF *ip;
  9756.   struct directive *kt;
  9757.   U_CHAR *buf, *p;
  9758.  
  9759.   p = buf = (U_CHAR *) str;
  9760.   if (!is_idstart[*p]) {
  9761.     error ("malformed option `-D %s'", str);
  9762.     return;
  9763.   }
  9764.   while (is_idchar[*++p])
  9765.     ;
  9766.   if (*p == '(') {
  9767.     while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
  9768.       ;
  9769.     if (*p++ != ')')
  9770.       p = (U_CHAR *) str;            /* Error */
  9771.   }
  9772.   if (*p == 0) {
  9773.     buf = (U_CHAR *) alloca (p - buf + 4);
  9774.     strcpy ((char *)buf, str);
  9775.     strcat ((char *)buf, " 1");
  9776.   } else if (*p != '=') {
  9777.     error ("malformed option `-D %s'", str);
  9778.     return;
  9779.   } else {
  9780.     U_CHAR *q;
  9781.     /* Copy the entire option so we can modify it.  */
  9782.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  9783.     strncpy ((char *) buf, str, p - (U_CHAR *) str);
  9784.     /* Change the = to a space.  */
  9785.     buf[p - (U_CHAR *) str] = ' ';
  9786.     /* Scan for any backslash-newline and remove it.  */
  9787.     p++;
  9788.     q = &buf[p - (U_CHAR *) str];
  9789.     while (*p) {
  9790.       if (*p == '\"' || *p == '\'') {
  9791.     int unterminated = 0;
  9792.     U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0,
  9793.                      NULL_PTR, NULL_PTR, &unterminated);
  9794.     if (unterminated)
  9795.       return;
  9796.     while (p != p1)
  9797.       if (*p == '\\' && p[1] == '\n')
  9798.         p += 2;
  9799.       else
  9800.         *q++ = *p++;
  9801.       } else if (*p == '\\' && p[1] == '\n')
  9802.     p += 2;
  9803.       /* Change newline chars into newline-markers.  */
  9804.       else if (*p == '\n')
  9805.     {
  9806.       *q++ = '\n';
  9807.       *q++ = '\n';
  9808.       p++;
  9809.     }
  9810.       else
  9811.     *q++ = *p++;
  9812.     }
  9813.     *q = 0;
  9814.   }
  9815.   
  9816.   ip = &instack[++indepth];
  9817.   ip->nominal_fname = ip->fname = "*Initialization*";
  9818.  
  9819.   ip->buf = ip->bufp = buf;
  9820.   ip->length = strlen ((char *) buf);
  9821.   ip->lineno = 1;
  9822.   ip->macro = 0;
  9823.   ip->free_ptr = 0;
  9824.   ip->if_stack = if_stack;
  9825.   ip->system_header_p = 0;
  9826.  
  9827.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  9828.     ;
  9829.  
  9830.   /* Pass NULL instead of OP, since this is a "predefined" macro.  */
  9831.   do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt);
  9832.   --indepth;
  9833. }
  9834.  
  9835. /* JF, this does the work for the -U option */
  9836.  
  9837. static void
  9838. make_undef (str, op)
  9839.      char *str;
  9840.      FILE_BUF *op;
  9841. {
  9842.   FILE_BUF *ip;
  9843.   struct directive *kt;
  9844.  
  9845.   ip = &instack[++indepth];
  9846.   ip->nominal_fname = ip->fname = "*undef*";
  9847.  
  9848.   ip->buf = ip->bufp = (U_CHAR *) str;
  9849.   ip->length = strlen (str);
  9850.   ip->lineno = 1;
  9851.   ip->macro = 0;
  9852.   ip->free_ptr = 0;
  9853.   ip->if_stack = if_stack;
  9854.   ip->system_header_p = 0;
  9855.  
  9856.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  9857.     ;
  9858.  
  9859.   do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt);
  9860.   --indepth;
  9861. }
  9862.  
  9863. /* Process the string STR as if it appeared as the body of a #assert.
  9864.    OPTION is the option name for which STR was the argument.  */
  9865.  
  9866. static void
  9867. make_assertion (option, str)
  9868.      char *option;
  9869.      char *str;
  9870. {
  9871.   FILE_BUF *ip;
  9872.   struct directive *kt;
  9873.   U_CHAR *buf, *p, *q;
  9874.  
  9875.   /* Copy the entire option so we can modify it.  */
  9876.   buf = (U_CHAR *) alloca (strlen (str) + 1);
  9877.   strcpy ((char *) buf, str);
  9878.   /* Scan for any backslash-newline and remove it.  */
  9879.   p = q = buf;
  9880.   while (*p) {
  9881.     if (*p == '\\' && p[1] == '\n')
  9882.       p += 2;
  9883.     else
  9884.       *q++ = *p++;
  9885.   }
  9886.   *q = 0;
  9887.  
  9888.   p = buf;
  9889.   if (!is_idstart[*p]) {
  9890.     error ("malformed option `%s %s'", option, str);
  9891.     return;
  9892.   }
  9893.   while (is_idchar[*++p])
  9894.     ;
  9895.   SKIP_WHITE_SPACE (p);
  9896.   if (! (*p == 0 || *p == '(')) {
  9897.     error ("malformed option `%s %s'", option, str);
  9898.     return;
  9899.   }
  9900.   
  9901.   ip = &instack[++indepth];
  9902.   ip->nominal_fname = ip->fname = "*Initialization*";
  9903.  
  9904.   ip->buf = ip->bufp = buf;
  9905.   ip->length = strlen ((char *) buf);
  9906.   ip->lineno = 1;
  9907.   ip->macro = 0;
  9908.   ip->free_ptr = 0;
  9909.   ip->if_stack = if_stack;
  9910.   ip->system_header_p = 0;
  9911.  
  9912.   for (kt = directive_table; kt->type != T_ASSERT; kt++)
  9913.     ;
  9914.  
  9915.   /* pass NULL as output ptr to do_define since we KNOW it never
  9916.      does any output.... */
  9917.   do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt);
  9918.   --indepth;
  9919. }
  9920.  
  9921. /* Append a chain of `struct file_name_list's
  9922.    to the end of the main include chain.
  9923.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  9924.  
  9925. static void
  9926. append_include_chain (first, last)
  9927.      struct file_name_list *first, *last;
  9928. {
  9929.   struct file_name_list *dir;
  9930.  
  9931.   if (!first || !last)
  9932.     return;
  9933.  
  9934.   if (include == 0)
  9935.     include = first;
  9936.   else
  9937.     last_include->next = first;
  9938.  
  9939.   if (first_bracket_include == 0)
  9940.     first_bracket_include = first;
  9941.  
  9942.   for (dir = first; ; dir = dir->next) {
  9943.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  9944.     if (len > max_include_len)
  9945.       max_include_len = len;
  9946.     if (dir == last)
  9947.       break;
  9948.   }
  9949.  
  9950.   last->next = NULL;
  9951.   last_include = last;
  9952. }
  9953.  
  9954. /* Add output to `deps_buffer' for the -M switch.
  9955.    STRING points to the text to be output.
  9956.    SPACER is ':' for targets, ' ' for dependencies.  */
  9957.  
  9958. static void
  9959. deps_output (string, spacer)
  9960.      char *string;
  9961.      int spacer;
  9962. {
  9963.   int size = strlen (string);
  9964.  
  9965.   if (size == 0)
  9966.     return;
  9967.  
  9968. #ifndef MAX_OUTPUT_COLUMNS
  9969. #define MAX_OUTPUT_COLUMNS 72
  9970. #endif
  9971.   if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size
  9972.       && 1 < deps_column) {
  9973.     bcopy (" \\\n ", &deps_buffer[deps_size], 4);
  9974.     deps_size += 4;
  9975.     deps_column = 1;
  9976.     if (spacer == ' ')
  9977.       spacer = 0;
  9978.   }
  9979.  
  9980.   if (deps_size + size + 8 > deps_allocated_size) {
  9981.     deps_allocated_size = (deps_size + size + 50) * 2;
  9982.     deps_buffer = xrealloc (deps_buffer, deps_allocated_size);
  9983.   }
  9984.   if (spacer == ' ') {
  9985.     deps_buffer[deps_size++] = ' ';
  9986.     deps_column++;
  9987.   }
  9988.   bcopy (string, &deps_buffer[deps_size], size);
  9989.   deps_size += size;
  9990.   deps_column += size;
  9991.   if (spacer == ':') {
  9992.     deps_buffer[deps_size++] = ':';
  9993.     deps_column++;
  9994.   }
  9995.   deps_buffer[deps_size] = 0;
  9996. }
  9997.  
  9998. static void
  9999. fatal (PRINTF_ALIST (msg))
  10000.      PRINTF_DCL (msg)
  10001. {
  10002.   va_list args;
  10003.  
  10004.   fprintf (stderr, "%s: ", progname);
  10005.   VA_START (args, msg);
  10006.   vfprintf (stderr, msg, args);
  10007.   va_end (args);
  10008.   fprintf (stderr, "\n");
  10009.   exit (FATAL_EXIT_CODE);
  10010. }
  10011.  
  10012. /* More 'friendly' abort that prints the line and file.
  10013.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  10014.  
  10015. void
  10016. fancy_abort ()
  10017. {
  10018.   fatal ("Internal gcc abort.");
  10019. }
  10020.  
  10021. static void
  10022. perror_with_name (name)
  10023.      char *name;
  10024. {
  10025.   fprintf (stderr, "%s: ", progname);
  10026.   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
  10027.   errors++;
  10028. }
  10029.  
  10030. static void
  10031. pfatal_with_name (name)
  10032.      char *name;
  10033. {
  10034.   perror_with_name (name);
  10035. #ifdef VMS
  10036.   exit (vaxc$errno);
  10037. #else
  10038.   exit (FATAL_EXIT_CODE);
  10039. #endif
  10040. }
  10041.  
  10042. /* Handler for SIGPIPE.  */
  10043.  
  10044. static void
  10045. pipe_closed (signo)
  10046.      /* If this is missing, some compilers complain.  */
  10047.      int signo;
  10048. {
  10049.   fatal ("output pipe has been closed");
  10050. }
  10051.  
  10052. static void
  10053. memory_full ()
  10054. {
  10055.   fatal ("Memory exhausted.");
  10056. }
  10057.  
  10058.  
  10059. GENERIC_PTR
  10060. xmalloc (size)
  10061.      size_t size;
  10062. {
  10063.   register GENERIC_PTR ptr = (GENERIC_PTR) malloc (size);
  10064.   if (!ptr)
  10065.     memory_full ();
  10066.   return ptr;
  10067. }
  10068.  
  10069. static GENERIC_PTR
  10070. xrealloc (old, size)
  10071.      GENERIC_PTR old;
  10072.      size_t size;
  10073. {
  10074.   register GENERIC_PTR ptr = (GENERIC_PTR) realloc (old, size);
  10075.   if (!ptr)
  10076.     memory_full ();
  10077.   return ptr;
  10078. }
  10079.  
  10080. static GENERIC_PTR
  10081. xcalloc (number, size)
  10082.      size_t number, size;
  10083. {
  10084.   register size_t total = number * size;
  10085.   register GENERIC_PTR ptr = (GENERIC_PTR) malloc (total);
  10086.   if (!ptr)
  10087.     memory_full ();
  10088.   bzero (ptr, total);
  10089.   return ptr;
  10090. }
  10091.  
  10092. static char *
  10093. savestring (input)
  10094.      char *input;
  10095. {
  10096.   size_t size = strlen (input);
  10097.   char *output = xmalloc (size + 1);
  10098.   strcpy (output, input);
  10099.   return output;
  10100. }
  10101.  
  10102. /* Get the file-mode and data size of the file open on FD
  10103.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  10104.  
  10105. static int
  10106. file_size_and_mode (fd, mode_pointer, size_pointer)
  10107.      int fd;
  10108.      int *mode_pointer;
  10109.      long int *size_pointer;
  10110. {
  10111.   struct stat sbuf;
  10112.  
  10113.   if (fstat (fd, &sbuf) < 0) return (-1);
  10114.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  10115.   if (size_pointer) *size_pointer = sbuf.st_size;
  10116.   return 0;
  10117. }
  10118.  
  10119. #ifdef VMS
  10120.  
  10121. /* Under VMS we need to fix up the "include" specification
  10122.    filename so that everything following the 1st slash is
  10123.    changed into its correct VMS file specification. */
  10124.  
  10125. static void
  10126. hack_vms_include_specification (fname)
  10127.      char *fname;
  10128. {
  10129.   register char *cp, *cp1, *cp2;
  10130.   int f, check_filename_before_returning, no_prefix_seen;
  10131.   char Local[512];
  10132.  
  10133.   check_filename_before_returning = 0;
  10134.   no_prefix_seen = 0;
  10135.  
  10136.   /* Ignore leading "./"s */
  10137.   while (fname[0] == '.' && fname[1] == '/') {
  10138.     strcpy (fname, fname+2);
  10139.     no_prefix_seen = 1;        /* mark this for later */
  10140.   }
  10141.   /* Look for the boundary between the VMS and UNIX filespecs */
  10142.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  10143.   if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto            */
  10144.   if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
  10145.   if (cp) {
  10146.     cp++;
  10147.   } else {
  10148.     cp = index (fname, '/');    /* Look for the "/" */
  10149.   }
  10150.  
  10151.   /*
  10152.    * Check if we have a vax-c style '#include filename'
  10153.    * and add the missing .h
  10154.    */
  10155.   if (cp == 0) {
  10156.     if (index(fname,'.') == 0)
  10157.       strcat(fname, ".h");
  10158.   } else {
  10159.     if (index(cp,'.') == 0)
  10160.       strcat(cp, ".h");
  10161.   }
  10162.  
  10163.   cp2 = Local;            /* initialize */
  10164.  
  10165.   /* We are trying to do a number of things here.  First of all, we are
  10166.      trying to hammer the filenames into a standard format, such that later
  10167.      processing can handle them.
  10168.      
  10169.      If the file name contains something like [dir.], then it recognizes this
  10170.      as a root, and strips the ".]".  Later processing will add whatever is
  10171.      needed to get things working properly.
  10172.      
  10173.      If no device is specified, then the first directory name is taken to be
  10174.      a device name (or a rooted logical). */
  10175.  
  10176.   /* See if we found that 1st slash */
  10177.   if (cp == 0) return;        /* Nothing to do!!! */
  10178.   if (*cp != '/') return;    /* Nothing to do!!! */
  10179.   /* Point to the UNIX filename part (which needs to be fixed!) */
  10180.   cp1 = cp+1;
  10181.   /* If the directory spec is not rooted, we can just copy
  10182.      the UNIX filename part and we are done */
  10183.   if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
  10184.     if (cp[-2] != '.') {
  10185.       /*
  10186.        * The VMS part ends in a `]', and the preceding character is not a `.'.
  10187.        * We strip the `]', and then splice the two parts of the name in the
  10188.        * usual way.  Given the default locations for include files in cccp.c,
  10189.        * we will only use this code if the user specifies alternate locations
  10190.        * with the /include (-I) switch on the command line.  */
  10191.       cp -= 1;            /* Strip "]" */
  10192.       cp1--;            /* backspace */
  10193.     } else {
  10194.       /*
  10195.        * The VMS part has a ".]" at the end, and this will not do.  Later
  10196.        * processing will add a second directory spec, and this would be a syntax
  10197.        * error.  Thus we strip the ".]", and thus merge the directory specs.
  10198.        * We also backspace cp1, so that it points to a '/'.  This inhibits the
  10199.        * generation of the 000000 root directory spec (which does not belong here
  10200.        * in this case).
  10201.        */
  10202.       cp -= 2;            /* Strip ".]" */
  10203.       cp1--; };            /* backspace */
  10204.   } else {
  10205.  
  10206.     /* We drop in here if there is no VMS style directory specification yet.
  10207.      * If there is no device specification either, we make the first dir a
  10208.      * device and try that.  If we do not do this, then we will be essentially
  10209.      * searching the users default directory (as if they did a #include "asdf.h").
  10210.      *
  10211.      * Then all we need to do is to push a '[' into the output string. Later
  10212.      * processing will fill this in, and close the bracket.
  10213.      */
  10214.     if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec.  take first dir */
  10215.     *cp2++ = '[';        /* Open the directory specification */
  10216.   }
  10217.  
  10218.   /* at this point we assume that we have the device spec, and (at least
  10219.      the opening "[" for a directory specification.  We may have directories
  10220.      specified already */
  10221.  
  10222.   /* If there are no other slashes then the filename will be
  10223.      in the "root" directory.  Otherwise, we need to add
  10224.      directory specifications. */
  10225.   if (index (cp1, '/') == 0) {
  10226.     /* Just add "000000]" as the directory string */
  10227.     strcpy (cp2, "000000]");
  10228.     cp2 += strlen (cp2);
  10229.     check_filename_before_returning = 1; /* we might need to fool with this later */
  10230.   } else {
  10231.     /* As long as there are still subdirectories to add, do them. */
  10232.     while (index (cp1, '/') != 0) {
  10233.       /* If this token is "." we can ignore it */
  10234.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  10235.     cp1 += 2;
  10236.     continue;
  10237.       }
  10238.       /* Add a subdirectory spec. Do not duplicate "." */
  10239.       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
  10240.     *cp2++ = '.';
  10241.       /* If this is ".." then the spec becomes "-" */
  10242.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  10243.     /* Add "-" and skip the ".." */
  10244.     *cp2++ = '-';
  10245.     cp1 += 3;
  10246.     continue;
  10247.       }
  10248.       /* Copy the subdirectory */
  10249.       while (*cp1 != '/') *cp2++= *cp1++;
  10250.       cp1++;            /* Skip the "/" */
  10251.     }
  10252.     /* Close the directory specification */
  10253.     if (cp2[-1] == '.')        /* no trailing periods */
  10254.       cp2--;
  10255.     *cp2++ = ']';
  10256.   }
  10257.   /* Now add the filename */
  10258.   while (*cp1) *cp2++ = *cp1++;
  10259.   *cp2 = 0;
  10260.   /* Now append it to the original VMS spec. */
  10261.   strcpy (cp, Local);
  10262.  
  10263.   /* If we put a [000000] in the filename, try to open it first. If this fails,
  10264.      remove the [000000], and return that name.  This provides flexibility
  10265.      to the user in that they can use both rooted and non-rooted logical names
  10266.      to point to the location of the file.  */
  10267.  
  10268.   if (check_filename_before_returning && no_prefix_seen) {
  10269.     f = open (fname, O_RDONLY, 0666);
  10270.     if (f >= 0) {
  10271.       /* The file name is OK as it is, so return it as is.  */
  10272.       close (f);
  10273.       return;
  10274.     }
  10275.     /* The filename did not work.  Try to remove the [000000] from the name,
  10276.        and return it.  */
  10277.     cp = index (fname, '[');
  10278.     cp2 = index (fname, ']') + 1;
  10279.     strcpy (cp, cp2);        /* this gets rid of it */
  10280.   }
  10281.   return;
  10282. }
  10283. #endif    /* VMS */
  10284.  
  10285. #ifdef    VMS
  10286.  
  10287. /* These are the read/write replacement routines for
  10288.    VAX-11 "C".  They make read/write behave enough
  10289.    like their UNIX counterparts that CCCP will work */
  10290.  
  10291. static int
  10292. read (fd, buf, size)
  10293.      int fd;
  10294.      char *buf;
  10295.      int size;
  10296. {
  10297. #undef    read    /* Get back the REAL read routine */
  10298.   register int i;
  10299.   register int total = 0;
  10300.  
  10301.   /* Read until the buffer is exhausted */
  10302.   while (size > 0) {
  10303.     /* Limit each read to 32KB */
  10304.     i = (size > (32*1024)) ? (32*1024) : size;
  10305.     i = read (fd, buf, i);
  10306.     if (i <= 0) {
  10307.       if (i == 0) return (total);
  10308.       return (i);
  10309.     }
  10310.     /* Account for this read */
  10311.     total += i;
  10312.     buf += i;
  10313.     size -= i;
  10314.   }
  10315.   return (total);
  10316. }
  10317.  
  10318. static int
  10319. write (fd, buf, size)
  10320.      int fd;
  10321.      char *buf;
  10322.      int size;
  10323. {
  10324. #undef    write    /* Get back the REAL write routine */
  10325.   int i;
  10326.   int j;
  10327.  
  10328.   /* Limit individual writes to 32Kb */
  10329.   i = size;
  10330.   while (i > 0) {
  10331.     j = (i > (32*1024)) ? (32*1024) : i;
  10332.     if (write (fd, buf, j) < 0) return (-1);
  10333.     /* Account for the data written */
  10334.     buf += j;
  10335.     i -= j;
  10336.   }
  10337.   return (size);
  10338. }
  10339.  
  10340. /* The following wrapper functions supply additional arguments to the VMS
  10341.    I/O routines to optimize performance with file handling.  The arguments
  10342.    are:
  10343.      "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
  10344.      "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
  10345.      "fop=tef"- Truncate unused portions of file when closing file.
  10346.      "shr=nil"- Disallow file sharing while file is open.
  10347.  */
  10348.  
  10349. static FILE *
  10350. freopen (fname, type, oldfile)
  10351.      char *fname;
  10352.      char *type;
  10353.      FILE *oldfile;
  10354. {
  10355. #undef    freopen    /* Get back the REAL fopen routine */
  10356.   if (strcmp (type, "w") == 0)
  10357.     return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  10358.   return freopen (fname, type, oldfile, "mbc=16");
  10359. }
  10360.  
  10361. static FILE *
  10362. fopen (fname, type)
  10363.      char *fname;
  10364.      char *type;
  10365. {
  10366. #undef fopen    /* Get back the REAL fopen routine */
  10367.   /* The gcc-vms-1.42 distribution's header files prototype fopen with two
  10368.      fixed arguments, which matches ANSI's specification but not VAXCRTL's
  10369.      pre-ANSI implementation.  This hack circumvents the mismatch problem.  */
  10370.   FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen;
  10371.  
  10372.   if (*type == 'w')
  10373.     return (*vmslib_fopen) (fname, type, "mbc=32",
  10374.                 "deq=64", "fop=tef", "shr=nil");
  10375.   else
  10376.     return (*vmslib_fopen) (fname, type, "mbc=32");
  10377. }
  10378.  
  10379. static int 
  10380. open (fname, flags, prot)
  10381.      char *fname;
  10382.      int flags;
  10383.      int prot;
  10384. {
  10385. #undef open    /* Get back the REAL open routine */
  10386.   return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
  10387. }
  10388.  
  10389. /* Avoid run-time library bug, where copying M out of N+M characters with
  10390.    N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
  10391.    gcc-cpp exercises this particular bug.  [Fixed in V5.5-2's VAXCRTL.]  */
  10392.  
  10393. static char *
  10394. strncat (dst, src, cnt)
  10395.      char *dst;
  10396.      const char *src;
  10397.      unsigned cnt;
  10398. {
  10399.   register char *d = dst, *s = (char *) src;
  10400.   register int n = cnt;    /* convert to _signed_ type */
  10401.  
  10402.   while (*d) d++;    /* advance to end */
  10403.   while (--n >= 0)
  10404.     if (!(*d++ = *s++)) break;
  10405.   if (n < 0) *d = '\0';
  10406.   return dst;
  10407. }
  10408.  
  10409. /* more VMS hackery */
  10410. #include <fab.h>
  10411. #include <nam.h>
  10412.  
  10413. extern unsigned long sys$parse(), sys$search();
  10414.  
  10415. /* Work around another library bug.  If a file is located via a searchlist,
  10416.    and if the device it's on is not the same device as the one specified
  10417.    in the first element of that searchlist, then both stat() and fstat()
  10418.    will fail to return info about it.  `errno' will be set to EVMSERR, and
  10419.    `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
  10420.    We can get around this by fully parsing the filename and then passing
  10421.    that absolute name to stat().
  10422.  
  10423.    Without this fix, we can end up failing to find header files, which is
  10424.    bad enough, but then compounding the problem by reporting the reason for
  10425.    failure as "normal successful completion."  */
  10426.  
  10427. #undef fstat    /* get back to library version */
  10428.  
  10429. static int
  10430. VMS_fstat (fd, statbuf)
  10431.      int fd;
  10432.      struct stat *statbuf;
  10433. {
  10434.   int result = fstat (fd, statbuf);
  10435.  
  10436.   if (result < 0)
  10437.     {
  10438.       FILE *fp;
  10439.       char nambuf[NAM$C_MAXRSS+1];
  10440.  
  10441.       if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0)
  10442.     result = VMS_stat (nambuf, statbuf);
  10443.       /* No fclose(fp) here; that would close(fd) as well.  */
  10444.     }
  10445.  
  10446.   return result;
  10447. }
  10448.  
  10449. static int
  10450. VMS_stat (name, statbuf)
  10451.      const char *name;
  10452.      struct stat *statbuf;
  10453. {
  10454.   int result = stat (name, statbuf);
  10455.  
  10456.   if (result < 0)
  10457.     {
  10458.       struct FAB fab;
  10459.       struct NAM nam;
  10460.       char exp_nam[NAM$C_MAXRSS+1],  /* expanded name buffer for sys$parse */
  10461.        res_nam[NAM$C_MAXRSS+1];  /* resultant name buffer for sys$search */
  10462.  
  10463.       fab = cc$rms_fab;
  10464.       fab.fab$l_fna = (char *) name;
  10465.       fab.fab$b_fns = (unsigned char) strlen (name);
  10466.       fab.fab$l_nam = (void *) &nam;
  10467.       nam = cc$rms_nam;
  10468.       nam.nam$l_esa = exp_nam,  nam.nam$b_ess = sizeof exp_nam - 1;
  10469.       nam.nam$l_rsa = res_nam,  nam.nam$b_rss = sizeof res_nam - 1;
  10470.       nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL;
  10471.       if (sys$parse (&fab) & 1)
  10472.     {
  10473.       if (sys$search (&fab) & 1)
  10474.         {
  10475.           res_nam[nam.nam$b_rsl] = '\0';
  10476.           result = stat (res_nam, statbuf);
  10477.         }
  10478.       /* Clean up searchlist context cached by the system.  */
  10479.       nam.nam$b_nop = NAM$M_SYNCHK;
  10480.       fab.fab$l_fna = 0,  fab.fab$b_fns = 0;
  10481.       (void) sys$parse (&fab);
  10482.     }
  10483.     }
  10484.  
  10485.   return result;
  10486. }
  10487. #endif /* VMS */
  10488.